HERE Map Turn By Turn Activate - android

i'm developing turn by turn based navigation application on android. I'm using HERE maps sdk for android. I can't switch to navigation driver mode for turn by turn navigation. How can i switch normal map to turn by turn mode.
Note: I changed to map scheme mode
map.setMapScheme(Map.Scheme.CARNAV_TRAFFIC_DAY);
public class BasicMapActivity extends Activity {
// permissions request code
private final static int REQUEST_CODE_ASK_PERMISSIONS = 1;
private PositioningManager posManager;
private MapRoute mapRoute;
private Route route;
private GeoPosition myPosition;
/**
* Permissions that need to be explicitly requested from end user.
*/
private static final String[] REQUIRED_SDK_PERMISSIONS = new String[] {
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE };
// map embedded in the map fragment
private Map map = null;
// map fragment embedded in this activity
private MapFragment mapFragment = null;
private PositioningManager.OnPositionChangedListener positionListener = new PositioningManager.OnPositionChangedListener() {
#Override
public void onPositionUpdated(PositioningManager.LocationMethod locationMethod, GeoPosition geoPosition, boolean b) {
Log.v("HERE MAP",geoPosition.getCoordinate().toString());
myPosition = geoPosition;
map.setCenter(geoPosition.getCoordinate(),
Map.Animation.BOW);
// Set the zoom level to the average between min and max
}
#Override
public void onPositionFixChanged(PositioningManager.LocationMethod locationMethod, PositioningManager.LocationStatus locationStatus) {
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkPermissions();
}
#Override
protected void onResume() {
super.onResume();
if (posManager != null) {
posManager.start(
PositioningManager.LocationMethod.GPS_NETWORK);
}
}
#Override
protected void onPause() {
super.onPause();
if (posManager != null) {
posManager.stop();
}
}
public void onDestroy() {
if (posManager != null) {
// Cleanup
posManager.removeListener(
positionListener);
}
map = null;
super.onDestroy();
}
private void initialize() {
setContentView(R.layout.activity_main);
findViewById(R.id.btnDrawRoute).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawRoute();
}
});
findViewById(R.id.btnChangeMap).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setNavigationManager();
NavigationManager.Error error = NavigationManager.getInstance().startNavigation(route);
if(error.equals(NavigationManager.Error.NONE)){
map.setExtrudedBuildingsVisible(false);
}
}
});
// Search for the map fragment to finish setup by calling init().
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapfragment);
mapFragment.init(new OnEngineInitListener() {
#Override
public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
if (error == OnEngineInitListener.Error.NONE) {
// retrieve a reference of the map from the map fragment
map = mapFragment.getMap();
// Set the map center to the Vancouver region (no animation)
map.setZoomLevel((map.getMaxZoomLevel() + map.getMinZoomLevel()) / 2);
map.setCartoMarkersVisible(true);
map.setLandmarksVisible(true);
map.setMapScheme(Map.Scheme.CARNAV_DAY);
map.setTrafficInfoVisible(true);
map.setExtrudedBuildingsVisible(true);
map.setStreetLevelCoverageVisible(true);
mapFragment.getPositionIndicator().setVisible(true);
try {
posManager = PositioningManager.getInstance();
posManager.addListener(new WeakReference<>(positionListener));
posManager.start(
PositioningManager.LocationMethod.GPS_NETWORK);
}catch (Exception ex){
Log.v("Error",ex.getMessage());
}
} else {
System.out.println("ERROR: Cannot initialize Map Fragment");
}
}
});
}
private void setNavigationManager() {
NavigationManager.getInstance().setMap(map);
NavigationManager.getInstance().setMapUpdateMode(NavigationManager.MapUpdateMode.ROADVIEW);
NavigationManager.getInstance().setTrafficAvoidanceMode(NavigationManager.TrafficAvoidanceMode.DYNAMIC);
NavigationManager.getInstance().setRealisticViewMode(NavigationManager.RealisticViewMode.DAY);
NavigationManager.getInstance().setDistanceUnit(NavigationManager.UnitSystem.METRIC);
ArrayList<NavigationManager.NaturalGuidanceMode> arrayList = new ArrayList<NavigationManager.NaturalGuidanceMode>();
arrayList.add(NavigationManager.NaturalGuidanceMode.JUNCTION);
arrayList.add(NavigationManager.NaturalGuidanceMode.STOP_SIGN);
arrayList.add(NavigationManager.NaturalGuidanceMode.TRAFFIC_LIGHT);
EnumSet<NavigationManager.NaturalGuidanceMode> enumSet = EnumSet.copyOf(arrayList);
NavigationManager.getInstance().setNaturalGuidanceMode(enumSet);
setVoice();
ArrayList<NavigationManager.AudioEvent> audioEvents = new ArrayList<>();
audioEvents.add(NavigationManager.AudioEvent.ROUTE);
audioEvents.add(NavigationManager.AudioEvent.GPS);
NavigationManager.getInstance().setEnabledAudioEvents(EnumSet.copyOf(audioEvents));
NavigationManager.getInstance().addNewInstructionEventListener(new WeakReference<NavigationManager.NewInstructionEventListener>(new NavigationManager.NewInstructionEventListener() {
#Override
public void onNewInstructionEvent() {
super.onNewInstructionEvent();
Maneuver maneuver = NavigationManager.getInstance().getNextManeuver();
Log.v("Manevra",maneuver.getNextRoadName() + " "+maneuver.getRoadName());
maneuver.getRoadName();
}
}));
NavigationManager.getInstance().addManeuverEventListener(new WeakReference<NavigationManager.ManeuverEventListener>(new NavigationManager.ManeuverEventListener() {
#Override
public void onManeuverEvent() {
super.onManeuverEvent();
}
}));
NavigationManager.getInstance().addPositionListener(new WeakReference<NavigationManager.PositionListener>(new NavigationManager.PositionListener() {
#Override
public void onPositionUpdated(GeoPosition geoPosition) {
super.onPositionUpdated(geoPosition);
geoPosition.getCoordinate();
geoPosition.getHeading();
geoPosition.getSpeed();
// also remaining time and distance can be
// fetched from navigation manager
NavigationManager.getInstance().getTta(Route.TrafficPenaltyMode.DISABLED, true);
NavigationManager.getInstance().getDestinationDistance();
}
}));
}
private void setVoice() {
// Get the list of voice packages from the voice catalog list
List<VoicePackage> voicePackages = VoiceCatalog.getInstance().getCatalogList();
long id = -1;
// select
for (VoicePackage vPackage : voicePackages) {
if (vPackage.getMarcCode().compareToIgnoreCase("eng") == 0) {
if (vPackage.isTts()) {
id = vPackage.getId();
break;
}
}
}
if(id>-1)
NavigationManager.getInstance().setVoiceSkin(VoiceCatalog.getInstance().getLocalVoiceSkin(id));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/**
* Checks the dynamically controlled permissions and requests missing permissions from end user.
*/
protected void checkPermissions() {
final List<String> missingPermissions = new ArrayList<String>();
// check all required dynamic permissions
for (final String permission : REQUIRED_SDK_PERMISSIONS) {
final int result = ContextCompat.checkSelfPermission(this, permission);
if (result != PackageManager.PERMISSION_GRANTED) {
missingPermissions.add(permission);
}
}
if (!missingPermissions.isEmpty()) {
// request all missing permissions
final String[] permissions = missingPermissions
.toArray(new String[missingPermissions.size()]);
ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE_ASK_PERMISSIONS);
} else {
final int[] grantResults = new int[REQUIRED_SDK_PERMISSIONS.length];
Arrays.fill(grantResults, PackageManager.PERMISSION_GRANTED);
onRequestPermissionsResult(REQUEST_CODE_ASK_PERMISSIONS, REQUIRED_SDK_PERMISSIONS,
grantResults);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String permissions[],
#NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
for (int index = permissions.length - 1; index >= 0; --index) {
if (grantResults[index] != PackageManager.PERMISSION_GRANTED) {
// exit the app if one permission is not granted
Toast.makeText(this, "Required permission '" + permissions[index]
+ "' not granted, exiting", Toast.LENGTH_LONG).show();
finish();
return;
}
}
// all permissions were granted
initialize();
break;
}
}
private void drawRoute(){
CoreRouter router = new CoreRouter();
GeoCoordinate gebzeCoord = new GeoCoordinate(40.9047617,29.4065243);
GeoCoordinate maslakCoord = new GeoCoordinate(41.1066509,29.0204711);
// Create the RoutePlan and add two waypoints
RoutePlan routePlan = new RoutePlan();
routePlan.addWaypoint(new RouteWaypoint(myPosition.getCoordinate()));
routePlan.addWaypoint(new RouteWaypoint(maslakCoord));
// Create the RouteOptions and set its transport mode & routing type
RouteOptions routeOptions = new RouteOptions();
routeOptions.setTransportMode(RouteOptions.TransportMode.CAR);
routeOptions.setRouteType(RouteOptions.Type.FASTEST);
routePlan.setRouteOptions(routeOptions);
// Calculate the route
router.calculateRoute(routePlan, new RouteListener());
TrafficUpdater trafficUpdater = TrafficUpdater.getInstance();
trafficUpdater.request(route, new TrafficUpdater.Listener() {
#Override
public void onStatusChanged(TrafficUpdater.RequestState requestState) {
}
});
}
private class RouteListener implements CoreRouter.Listener {
// Method defined in Listener
public void onProgress(int percentage) {
// Display a message indicating calculation progress
}
// Method defined in Listener
public void onCalculateRouteFinished(List<RouteResult> routeResult, RoutingError error) {
// If the route was calculated successfully
if (error == RoutingError.NONE) {
// Render the route on the map
mapRoute = new MapRoute(routeResult.get(0).getRoute());
route = routeResult.get(0).getRoute();
map.addMapObject(mapRoute);
}
else {
// Display a message indicating route calculation failure
}
}
}
}

Declare NavigationManager's object as a global object.
private NavigationManager m_navigationManager;
after that initialize it in your setNavigationManager() method.
m_navigationManager = NavigationManager.getInstance();
m_navigationManager.setMap(map);
// try to change current MapUpdateMode to POSITION_ANIMATION
m_navigationManager.setMapUpdateMode(NavigationManager.MapUpdateMode.POSITION_ANIMATION);
m_navigationManager.setTrafficAvoidanceMode(NavigationManager.TrafficAvoidanceMode.DYNAMIC);
m_navigationManager.setRealisticViewMode(NavigationManager.RealisticViewMode.DAY);
// set all other method on this object.
m_navigationManager.......
After you have to start navigation like this
NavigationManager.Error error = m_navigationManager.startNavigation(route);
if (error != NavigationManager.Error.NONE) {
m_navigationManager.setMap(null);
}
//set Tilt to at some angle that you can look in 3d mode
map.setTilt(70f, com.here.android.mpa.mapping.Map.Animation.NONE);
map.setZoomLevel(18);

Related

android new autocomplete place sdk is unable to shown all locations

I implemented the new autocomplete place sdk in an Android app but it does not show most of places for Nigeria. For example, when I enter spar PH it should show the location SPAR PH, Forces Avenue, Port Harcourt, Nigeria in the suggestion but doesn't. There are other examples that aren't working, too. This was working with the autocomplete Place API but not the new autocomplete .
activity code
public class LocationPickActivity extends BaseActivity
implements OnMapReadyCallback,
GoogleMap.OnCameraMoveListener,
GoogleMap.OnCameraIdleListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationPickIView {
private static final LatLngBounds BOUNDS_INDIA = new LatLngBounds(new LatLng(-0, 0), new LatLng(0, 0));
private Location mLastKnownLocation;
protected GoogleApiClient mGoogleApiClient;
#BindView(R.id.appbar)
AppBarLayout appbar;
#BindView(R.id.toolbar)
Toolbar toolbar;
#BindView(R.id.source)
EditText source;
#BindView(R.id.destination)
EditText destination;
#BindView(R.id.destination_layout)
LinearLayout destinationLayout;
#BindView(R.id.home_address_layout)
LinearLayout homeAddressLayout;
#BindView(R.id.work_address_layout)
LinearLayout workAddressLayout;
#BindView(R.id.home_address)
TextView homeAddress;
#BindView(R.id.work_address)
TextView workAddress;
#BindView(R.id.locations_rv)
RecyclerView locationsRv;
#BindView(R.id.location_bs_layout)
CardView locationBsLayout;
#BindView(R.id.dd)
CoordinatorLayout dd;
boolean isEnableIdle = false;
#BindView(R.id.llSource)
LinearLayout llSource;
private boolean isLocationRvClick = false;
private boolean isSettingLocationClick = false;
private boolean mLocationPermissionGranted;
private GoogleMap mGoogleMap;
private String s_address;
private Double s_latitude;
private Double s_longitude;
private FusedLocationProviderClient mFusedLocationProviderClient;
private BottomSheetBehavior mBottomSheetBehavior;
private Boolean isEditable = true;
private UserAddress home, work = null;
private LocationPickPresenter<LocationPickActivity> presenter = new LocationPickPresenter<>();
private EditText selectedEditText;
private PlacesAutoCompleteAdapter mAutoCompleteAdapter;
//Base on action we are show/hide view and setResults
private String actionName = Constants.LocationActions.SELECT_SOURCE;
PlacesClient placesClient =null;
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
if (isEditable) if (!s.toString().equals("") && mGoogleApiClient.isConnected()) {
locationsRv.setVisibility(View.VISIBLE);
mAutoCompleteAdapter.getFilter().filter(s.toString());
if (mBottomSheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED)
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
} else if (!mGoogleApiClient.isConnected()) Log.e("ERROR", "API_NOT_CONNECTED");
if (s.toString().equals("")) locationsRv.setVisibility(View.GONE);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
};
#Override
public int getLayoutId() {
return R.layout.activity_location_pick;
}
#Override
public void initView() {
buildGoogleApiClient();
ButterKnife.bind(this);
presenter.attachView(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Places.initialize(getApplicationContext(), getString(R.string.google_map_key));
placesClient = Places.createClient(this);
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mBottomSheetBehavior = BottomSheetBehavior.from(locationBsLayout);
mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
}
});
mAutoCompleteAdapter = new PlacesAutoCompleteAdapter(this, R.layout.list_item_location, mGoogleApiClient, BOUNDS_INDIA);
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this);
locationsRv.setLayoutManager(mLinearLayoutManager);
locationsRv.setAdapter(mAutoCompleteAdapter);
source.addTextChangedListener(filterTextWatcher);
destination.addTextChangedListener(filterTextWatcher);
source.setOnFocusChangeListener((view, hasFocus) -> {
if (hasFocus) selectedEditText = source;
});
destination.setOnFocusChangeListener((view, hasFocus) -> {
if (hasFocus) selectedEditText = destination;
});
destination.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
setResult(Activity.RESULT_OK, new Intent());
finish();
return true;
}
return false;
});
source.setText(MvpApplication.RIDE_REQUEST.containsKey(Constants.RIDE_REQUEST.SRC_ADD)
? TextUtils.isEmpty(Objects.requireNonNull(MvpApplication.RIDE_REQUEST.get(Constants.RIDE_REQUEST.SRC_ADD)).toString())
? ""
: String.valueOf(MvpApplication.RIDE_REQUEST.get(Constants.RIDE_REQUEST.SRC_ADD))
: "");
destination.setText(MvpApplication.RIDE_REQUEST.containsKey(Constants.RIDE_REQUEST.DEST_ADD)
? TextUtils.isEmpty(Objects.requireNonNull(MvpApplication.RIDE_REQUEST.get(Constants.RIDE_REQUEST.DEST_ADD)).toString())
? ""
: String.valueOf(MvpApplication.RIDE_REQUEST.get(Constants.RIDE_REQUEST.DEST_ADD))
: "");
locationsRv.addOnItemTouchListener(new RecyclerItemClickListener(this, (view, position) -> {
if (mAutoCompleteAdapter.getItemCount() == 0) return;
final PlacesAutoCompleteAdapter.PlaceAutocomplete item = mAutoCompleteAdapter.getItem(position);
final String placeId = String.valueOf(item.placeId);
Log.i("LocationPickActivity", "Autocomplete item selected: " + item.address);
List<Field> placeFields = Arrays.asList(Field.LAT_LNG);
);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
actionName = bundle.getString("actionName",Constants.LocationActions.SELECT_SOURCE);
if (!TextUtils.isEmpty(actionName) && actionName.equalsIgnoreCase(Constants.LocationActions.SELECT_SOURCE)) {
destination.setCursorVisible(false);
source.setCursorVisible(true);
source.requestFocus();
selectedEditText = source;
}else if (!TextUtils.isEmpty(actionName) && actionName.equalsIgnoreCase(Constants.LocationActions.SELECT_DESTINATION)){
source.setCursorVisible(false);
destination.setCursorVisible(true);
destination.setText("");
destination.requestFocus();
selectedEditText = destination;
}else if (!TextUtils.isEmpty(actionName) && actionName.equals(Constants.LocationActions.CHANGE_DESTINATION)){
llSource.setVisibility(View.GONE);
source.setHint(getString(R.string.select_location));
selectedEditText = destination;
}else if (!TextUtils.isEmpty(actionName) && (actionName.equals(Constants.LocationActions.SELECT_HOME)|| actionName.equals(Constants.LocationActions.SELECT_WORK))){
destinationLayout.setVisibility(View.GONE);
selectedEditText = destination;
source.setText("");
source.setHint(getString(R.string.select_location));
} else{
destinationLayout.setVisibility(View.VISIBLE);
llSource.setVisibility(View.VISIBLE);
source.setHint(getString(R.string.pickup_location));
selectedEditText = source;
}
}
presenter.address();
}
private void setLocationText(String address, LatLng latLng, boolean isLocationRvClick,
boolean isSettingLocationClick) {
if (address != null && latLng != null) {
isEditable = false;
selectedEditText.setText(address);
isEditable = true;
if (selectedEditText.getTag().equals("source")) {
s_address = address;
s_latitude = latLng.latitude;
s_longitude = latLng.longitude;
MvpApplication.RIDE_REQUEST.put(Constants.RIDE_REQUEST.SRC_ADD, address);
MvpApplication.RIDE_REQUEST.put(Constants.RIDE_REQUEST.SRC_LAT, latLng.latitude);
MvpApplication.RIDE_REQUEST.put(Constants.RIDE_REQUEST.SRC_LONG, latLng.longitude);
}
if (selectedEditText.getTag().equals("destination")) {
MvpApplication.RIDE_REQUEST.put(Constants.RIDE_REQUEST.DEST_ADD, address);
MvpApplication.RIDE_REQUEST.put(Constants.RIDE_REQUEST.DEST_LAT, latLng.latitude);
MvpApplication.RIDE_REQUEST.put(Constants.RIDE_REQUEST.DEST_LONG, latLng.longitude);
if (isLocationRvClick) {
// Done functionality called...
setResult(Activity.RESULT_OK, new Intent());
finish();
}
}
} else {
isEditable = false;
selectedEditText.setText("");
locationsRv.setVisibility(View.GONE);
isEditable = true;
if (selectedEditText.getTag().equals("source")) {
MvpApplication.RIDE_REQUEST.remove(Constants.RIDE_REQUEST.SRC_ADD);
MvpApplication.RIDE_REQUEST.remove(Constants.RIDE_REQUEST.SRC_LAT);
MvpApplication.RIDE_REQUEST.remove(Constants.RIDE_REQUEST.SRC_LONG);
}
if (selectedEditText.getTag().equals("destination")) {
MvpApplication.RIDE_REQUEST.remove(Constants.RIDE_REQUEST.DEST_ADD);
MvpApplication.RIDE_REQUEST.remove(Constants.RIDE_REQUEST.DEST_LAT);
MvpApplication.RIDE_REQUEST.remove(Constants.RIDE_REQUEST.DEST_LONG);
}
}
if (isSettingLocationClick) {
hideKeyboard();
locationsRv.setVisibility(View.GONE);
}
}
#Override
public void onResume() {
super.onResume();
if (!mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting()) {
Log.v("Google API", "Connecting");
mGoogleApiClient.connect();
}
}
#Override
public void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
Log.v("Google API", "Dis-Connecting");
mGoogleApiClient.disconnect();
}
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
#OnClick({R.id.source, R.id.destination, R.id.reset_source, R.id.reset_destination, R.id.home_address_layout, R.id.work_address_layout})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.source:
break;
case R.id.destination:
break;
case R.id.reset_source:
selectedEditText = source;
source.requestFocus();
setLocationText(null, null, isLocationRvClick, isSettingLocationClick);
break;
case R.id.reset_destination:
destination.requestFocus();
selectedEditText = destination;
setLocationText(null, null, isLocationRvClick, isSettingLocationClick);
break;
case R.id.home_address_layout:
if (home != null)
setLocationText(home.getAddress(),
new LatLng(home.getLatitude(), home.getLongitude()),
isLocationRvClick, isSettingLocationClick);
break;
case R.id.work_address_layout:
if (work != null)
setLocationText(work.getAddress(),
new LatLng(work.getLatitude(), work.getLongitude()),
isLocationRvClick, isSettingLocationClick);
break;
}
}
#Override
public void onCameraIdle() {
try {
CameraPosition cameraPosition = mGoogleMap.getCameraPosition();
if (isEnableIdle) {
String address = getAddress(cameraPosition.target);
System.out.println("onCameraIdle " + address);
hideKeyboard();
setLocationText(address, cameraPosition.target, isLocationRvClick, isSettingLocationClick);
}
isEnableIdle = true;
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onCameraMove() {
System.out.println("LocationPickActivity.onCameraMove");
}
#Override
public void onMapReady(GoogleMap googleMap) {
try {
// Google map custom style...
googleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this, R.raw.style_json));
} catch (Resources.NotFoundException e) {
Log.d("Map:Style", "Can't find style. Error: ");
}
this.mGoogleMap = googleMap;
getLocationPermission();
updateLocationUI();
getDeviceLocation();
}
void getDeviceLocation() {
try {
if (mLocationPermissionGranted) {
Task<Location> locationResult = mFusedLocationProviderClient.getLastLocation();
locationResult.addOnCompleteListener(this, task -> {
if (task.isSuccessful() && task.getResult() != null) {
mLastKnownLocation = task.getResult();
mGoogleMap.moveCamera(CameraUpdateFactory
.newLatLngZoom(new LatLng(
mLastKnownLocation.getLatitude(),
mLastKnownLocation.getLongitude()
), DEFAULT_ZOOM));
} else {
Log.d("Map", "Current location is null. Using defaults.");
Log.e("Map", "Exception: %s", task.getException());
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mDefaultLocation, DEFAULT_ZOOM));
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false);
}
});
}
} catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage());
}
}
public void getLocationPermission() {
if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
mLocationPermissionGranted = true;
else
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_ACCESS_FINE_LOCATION);
}
private void updateLocationUI() {
if (mGoogleMap == null) return;
try {
if (mLocationPermissionGranted) {
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false);
mGoogleMap.setOnCameraMoveListener(this);
mGoogleMap.setOnCameraIdleListener(this);
} else {
mGoogleMap.setMyLocationEnabled(false);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false);
mLastKnownLocation = null;
getLocationPermission();
}
} catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage());
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String permissions[], #NonNull int[] grantResults) {
mLocationPermissionGranted = false;
if (requestCode == REQUEST_ACCESS_FINE_LOCATION)
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mLocationPermissionGranted = true;
updateLocationUI();
getDeviceLocation();
}
}
#Override
public void onConnected(#Nullable Bundle bundle) {
}
#Override
public void onConnectionSuspended(int i) {
Log.v("Google API Callback", "Connection Suspended");
Log.v("Code", String.valueOf(i));
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
Log.v("Error Code", String.valueOf(connectionResult.getErrorCode()));
Toast.makeText(this, "API_NOT_CONNECTED", Toast.LENGTH_SHORT).show();
}
#Override
public void onBackPressed() {
if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED)
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
else super.onBackPressed();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.location_pick_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_done:
if (!TextUtils.isEmpty(actionName) && actionName.equals(Constants.LocationActions.SELECT_HOME) || actionName.equals(Constants.LocationActions.SELECT_WORK)){
Intent intent = new Intent();
intent.putExtra(Constants.RIDE_REQUEST.SRC_ADD, s_address);
intent.putExtra(Constants.RIDE_REQUEST.SRC_LAT, s_latitude);
intent.putExtra(Constants.RIDE_REQUEST.SRC_LONG, s_longitude);
setResult(Activity.RESULT_OK, intent);
finish();
} else {
setResult(Activity.RESULT_OK, new Intent());
finish();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onSuccess(AddressResponse address) {
if (address.getHome().isEmpty()) homeAddressLayout.setVisibility(View.GONE);
else {
home = address.getHome().get(address.getHome().size() - 1);
homeAddress.setText(home.getAddress());
homeAddressLayout.setVisibility(View.VISIBLE);
}
if (address.getWork().isEmpty()) workAddressLayout.setVisibility(View.GONE);
else {
work = address.getWork().get(address.getWork().size() - 1);
workAddress.setText(work.getAddress());
workAddressLayout.setVisibility(View.VISIBLE);
}
}
#Override
public void onError(Throwable e) {
handleError(e);
}
#Override
protected void onDestroy() {
presenter.onDetach();
super.onDestroy();
}
}
public class PlacesAutoCompleteAdapter extends RecyclerView.Adapter<PlacesAutoCompleteAdapter.PredictionHolder> implements Filterable {
private static final String TAG = "PlacesAutoAdapter";
private ArrayList<PlaceAutocomplete> mResultList = new ArrayList<>();
private GoogleApiClient mGoogleApiClient;
private LatLngBounds mBounds;
private Context mContext;
private int layout;
private CharacterStyle STYLE_BOLD;
private CharacterStyle STYLE_NORMAL;
PlacesClient placesClient =null;
public PlacesAutoCompleteAdapter(Context context, int resource, GoogleApiClient googleApiClient, LatLngBounds bounds) {
mContext = context;
layout = resource;
mGoogleApiClient = googleApiClient;
mBounds = bounds;
STYLE_BOLD = new StyleSpan(Typeface.BOLD);
STYLE_NORMAL = new StyleSpan(Typeface.NORMAL);
if (!Places.isInitialized()) {
Places.initialize(context, context.getString(R.string.google_api_key));
}
placesClient = Places.createClient(context);
}
public void setBounds(LatLngBounds bounds) {
mBounds = bounds;
}
/**
* Returns the filter for the current set of autocomplete results.
*/
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// Skip the autocomplete query if no constraints are given.
if (constraint != null) {
// Query the autocomplete API for the (constraint) search string.
mResultList = getAutocomplete(constraint);
if (mResultList != null) {
// The API successfully returned results.
results.values = mResultList;
results.count = mResultList.size();
}
}
return results;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
// The API returned at least one result, update the data.
notifyDataSetChanged();
} else {
// The API did not return any results, invalidate the data set.
//notifyDataSetInvalidated();
}
}
};
}
private ArrayList getAutocomplete(CharSequence constraint) {
if (mGoogleApiClient.isConnected()) {
Log.i("", "Starting autocomplete query for: " + constraint);
// Create a new token for the autocomplete session. Pass this to FindAutocompletePredictionsRequest,
fetchPlace()).
AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();
FindAutocompletePredictionsRequest request = FindAutocompletePredictionsRequest.builder()
.setTypeFilter(TypeFilter.ADDRESS)
.setSessionToken(token)
.setQuery(constraint.toString())
.build();
ArrayList<PlaceAutocomplete> resultList = new ArrayList<>();
placesClient.findAutocompletePredictions(request).addOnSuccessListener((response) -> {
for (AutocompletePrediction prediction : response.getAutocompletePredictions()) {
Log.i(TAG, prediction.getPlaceId());
Log.i(TAG, prediction.getPrimaryText(null).toString());
resultList.add(new PlaceAutocomplete(prediction.getPlaceId(), prediction.getPrimaryText(STYLE_NORMAL),
prediction.getFullText(STYLE_BOLD)));
}
}).addOnFailureListener((exception) -> {
return resultList;
}
Log.e(TAG, "Google API client is not connected for autocomplete query.");
return null;
}
#NonNull
#Override
public PredictionHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = Objects.requireNonNull(layoutInflater).inflate(layout, viewGroup, false);
return new PredictionHolder(convertView);
}
#Override
public void onBindViewHolder(#NonNull PredictionHolder mPredictionHolder, final int i) {
mPredictionHolder.area.setText(mResultList.get(i).area);
mPredictionHolder.address.setText(mResultList.get(i).address.toString()
.replace(mResultList.get(i).area + ", ", ""));
}
#Override
public int getItemCount() {
return (mResultList == null) ? 0 : mResultList.size();
}
public PlaceAutocomplete getItem(int position) {
return mResultList.get(position);
}
class PredictionHolder extends RecyclerView.ViewHolder {
private TextView address, area;
PredictionHolder(View itemView) {
super(itemView);
area = itemView.findViewById(R.id.area);
address = itemView.findViewById(R.id.address);
}
}
public class PlaceAutocomplete {
public CharSequence placeId;
public CharSequence address, area;
PlaceAutocomplete(CharSequence placeId, CharSequence area, CharSequence address) {
this.placeId = placeId;
this.area = area;
this.address = address;
}
#Override
public String toString() {
return area.toString();
}
}
}
The Place Picker is deprecated as of January 29, 2019. This feature will be turned off on July 29, 2019, and will no longer be available after that date. You must install the compatibility library to continue using the Place Picker during the deprecation period. Once the deprecation period has ended, the Place Picker will no longer be available for use (including the version in the compatibility library).

onDestroy() is called when press home button in video call using agora.io

onDestroy() is called when press home button in video call using agora.io .
I have 2 project one is for user and other for astrologer. In User app everything in working fine but in astrologer app onDestroy is calling whenever I press the home button.
This is refernce for agora
https://github.com/AgoraIO/Basic-Video-Call/tree/master/One-to-One-Video/Agora-Android-Tutorial-1to1
public class VideoChatViewActivity extends AppCompatActivity {
private static final String TAG = VideoChatViewActivity.class.getSimpleName();
private static final int PERMISSION_REQ_ID = 22;
// Permission WRITE_EXTERNAL_STORAGE is not mandatory
// for Agora RTC SDK, just in case if you wanna save
// logs to external sdcard.
private static final String[] REQUESTED_PERMISSIONS = {
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
private RtcEngine mRtcEngine;
private boolean mCallEnd;
private boolean mMuted;
private FrameLayout mLocalContainer;
private RelativeLayout mRemoteContainer;
private SurfaceView mLocalView;
private SurfaceView mRemoteView;
private ImageView mCallBtn;
private ImageView mMuteBtn;
private ImageView mSwitchCameraBtn;
// Customized logger view
/**
* Event handler registered into RTC engine for RTC callbacks.
* Note that UI operations needs to be in UI thread because RTC
* engine deals with the events in a separate thread.
*/
private final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() {
#Override
public void onJoinChannelSuccess(String channel, final int uid, int elapsed) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Log.e("TAG", "Join channel success, uid: " + (uid & 0xFFFFFFFFL));
}
});
}
#Override
public void onFirstRemoteVideoDecoded(final int uid, int width, int height, int elapsed) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Log.e("TAG", "First remote video decoded, uid: " + (uid & 0xFFFFFFFFL));
setupRemoteVideo(uid);
}
});
}
#Override
public void onUserOffline(final int uid, int reason) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Log.e("TAG", "User offline, uid: " + (uid & 0xFFFFFFFFL));
onRemoteUserLeft();
}
});
}
};
private void setupRemoteVideo(int uid) {
// Only one remote video view is available for this
// tutorial. Here we check if there exists a surface
// view tagged as this uid.
int count = mRemoteContainer.getChildCount();
View view = null;
for (int i = 0; i < count; i++) {
View v = mRemoteContainer.getChildAt(i);
if (v.getTag() instanceof Integer && ((int) v.getTag()) == uid) {
view = v;
}
}
if (view != null) {
return;
}
mRemoteView = RtcEngine.CreateRendererView(getBaseContext());
mRemoteContainer.addView(mRemoteView);
mRtcEngine.setupRemoteVideo(new VideoCanvas(mRemoteView, VideoCanvas.RENDER_MODE_HIDDEN, uid));
mRemoteView.setTag(uid);
}
private void onRemoteUserLeft() {
removeRemoteVideo();
}
private void removeRemoteVideo() {
if (mRemoteView != null) {
mRemoteContainer.removeView(mRemoteView);
}
mRemoteView = null;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_chat_view);
initUI();
// Ask for permissions at runtime.
// This is just an example set of permissions. Other permissions
// may be needed, and please refer to our online documents.
if (checkSelfPermission(REQUESTED_PERMISSIONS[0], PERMISSION_REQ_ID) &&
checkSelfPermission(REQUESTED_PERMISSIONS[1], PERMISSION_REQ_ID) &&
checkSelfPermission(REQUESTED_PERMISSIONS[2], PERMISSION_REQ_ID)) {
initEngineAndJoinChannel();
}
}
private void initUI() {
mLocalContainer = findViewById(R.id.local_video_view_container);
mRemoteContainer = findViewById(R.id.remote_video_view_container);
mCallBtn = findViewById(R.id.btn_call);
mMuteBtn = findViewById(R.id.btn_mute);
mSwitchCameraBtn = findViewById(R.id.btn_switch_camera);
// Sample logs are optional.
showSampleLogs();
}
private void showSampleLogs() {
Log.e("TAG", "Welcome to Agora 1v1 video call");
}
private boolean checkSelfPermission(String permission, int requestCode) {
if (ContextCompat.checkSelfPermission(this, permission) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, REQUESTED_PERMISSIONS, requestCode);
return false;
}
return true;
}
#Override
public void onRequestPermissionsResult(int requestCode,
#NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQ_ID) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED ||
grantResults[1] != PackageManager.PERMISSION_GRANTED ||
grantResults[2] != PackageManager.PERMISSION_GRANTED) {
showLongToast("Need permissions " + Manifest.permission.RECORD_AUDIO +
"/" + Manifest.permission.CAMERA + "/" + Manifest.permission.WRITE_EXTERNAL_STORAGE);
finish();
return;
}
// Here we continue only if all permissions are granted.
// The permissions can also be granted in the system settings manually.
initEngineAndJoinChannel();
}
}
private void showLongToast(final String msg) {
this.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
});
}
private void initEngineAndJoinChannel() {
// This is our usual steps for joining
// a channel and starting a call.
initializeEngine();
setupVideoConfig();
setupLocalVideo();
joinChannel();
}
private void initializeEngine() {
try {
mRtcEngine = RtcEngine.create(getBaseContext(), getString(R.string.agora_app_id), mRtcEventHandler);
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
throw new RuntimeException("NEED TO check rtc sdk init fatal error\n" + Log.getStackTraceString(e));
}
}
private void setupVideoConfig() {
// In simple use cases, we only need to enable video capturing
// and rendering once at the initialization step.
// Note: audio recording and playing is enabled by default.
mRtcEngine.enableVideo();
// Please go to this page for detailed explanation
// https://docs.agora.io/en/Video/API%20Reference/java/classio_1_1agora_1_1rtc_1_1_rtc_engine.html#af5f4de754e2c1f493096641c5c5c1d8f
mRtcEngine.setVideoEncoderConfiguration(new VideoEncoderConfiguration(
VideoEncoderConfiguration.VD_640x360,
VideoEncoderConfiguration.FRAME_RATE.FRAME_RATE_FPS_15,
VideoEncoderConfiguration.STANDARD_BITRATE,
VideoEncoderConfiguration.ORIENTATION_MODE.ORIENTATION_MODE_FIXED_PORTRAIT));
}
private void setupLocalVideo() {
// This is used to set a local preview.
// The steps setting local and remote view are very similar.
// But note that if the local user do not have a uid or do
// not care what the uid is, he can set his uid as ZERO.
// Our server will assign one and return the uid via the event
// handler callback function (onJoinChannelSuccess) after
// joining the channel successfully.
mLocalView = RtcEngine.CreateRendererView(getBaseContext());
mLocalView.setZOrderMediaOverlay(true);
mLocalContainer.addView(mLocalView);
mRtcEngine.setupLocalVideo(new VideoCanvas(mLocalView, VideoCanvas.RENDER_MODE_HIDDEN, 0));
}
private void joinChannel() {
// 1. Users can only see each other after they join the
// same channel successfully using the same app id.
// 2. One token is only valid for the channel name that
// you use to generate this token.
mRtcEngine.joinChannel(null, getIntent().getExtras().getString("channel_name"), "Extra Optional Data", 0);
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy: ");
if (!mCallEnd) {
leaveChannel();
}
RtcEngine.destroy();
}
#Override
protected void onPause() {
super.onPause();
Log.e(TAG, "onPause: ");
}
#Override
protected void onStop() {
super.onStop();
Log.e(TAG, "onStop: ");
}
private void leaveChannel() {
mRtcEngine.leaveChannel();
}
public void onLocalAudioMuteClicked(View view) {
mMuted = !mMuted;
mRtcEngine.muteLocalAudioStream(mMuted);
int res = mMuted ? R.drawable.btn_mute : R.drawable.btn_mute;
mMuteBtn.setImageResource(res);
}
public void onSwitchCameraClicked(View view) {
mRtcEngine.switchCamera();
}
public void onCallClicked(View view) {
if (mCallEnd) {
startCall();
mCallEnd = false;
mCallBtn.setImageResource(R.drawable.btn_end_call);
} else {
endCall();
mCallEnd = true;
mCallBtn.setImageResource(R.drawable.btn_end_call);
}
showButtons(!mCallEnd);
}
private void startCall() {
setupLocalVideo();
joinChannel();
}
private void endCall() {
removeLocalVideo();
removeRemoteVideo();
leaveChannel();
}
private void removeLocalVideo() {
if (mLocalView != null) {
mLocalContainer.removeView(mLocalView);
}
mLocalView = null;
}
private void showButtons(boolean show) {
int visibility = show ? View.VISIBLE : View.GONE;
mMuteBtn.setVisibility(visibility);
mSwitchCameraBtn.setVisibility(visibility);
}
}
Welcome to codding party.
you should implement
onSaveInstanceState
and
onRestoreInstanceState
properly. The framework may kill your Activity at any time it isn't foreground.
other way u can Use Foreground service for keeping alive your call.

Android runOnUiThread

I'm trying to get values through a query for the azure! After that I wanted to try to get these values and move to a new intent but I do not know how to do this because of threads
Here I start QrCode to find an id that I need
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.qrcode_activity);
cameraPreview = findViewById(R.id.cameraPreview);
txtResult = findViewById(R.id.txtresult);
barcodeDetector = new BarcodeDetector.Builder(this).
setBarcodeFormats(Barcode.QR_CODE).build();
cameraSource = new CameraSource.Builder(getApplicationContext(), barcodeDetector)
.setRequestedPreviewSize(640, 480).build();
intent = new Intent(getBaseContext(), ValuesActivity.class);
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
#Override
public void release() {
}
#Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> qrcodes = detections.getDetectedItems();
if(qrcodes.size() != 0)
{
txtResult.post(new Runnable() {
#Override
public void run() {
//Create vibrate
Vibrator vibrator = (Vibrator)getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(100);
_idDevice = qrcodes.valueAt(0).displayValue;
txtResult.setText(qrcodes.valueAt(0).displayValue);
getQuery();
}
});
}
}
});
cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
ActivityCompat.requestPermissions(QrCodeActivity.this, new String[]{Manifest.permission.CAMERA},RequestCameraPermissionID);
return;
}
try {
cameraSource.start(cameraPreview.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
}
This is where I do the query, however, as I call runUIThread I can not access the variables from that thread for another try.
As you can see:
public void getQuery(){
try
{
final ProgressDialog dialog = ProgressDialog.show(QrCodeActivity.this, "", "Loading. Please wait...", true);
Query query = Query.Companion.select()
.from("Equipment")
.where("deviceId", "Box2");
AzureData.queryDocuments("Equipment", "valuesDatabase", query, DictionaryDocument.class,null, onCallback( response -> {
Log.e(TAG, "Document list result: " + response.isSuccessful());
idnomes = new ArrayList<>();
listaValues = new ArrayList<>();
listaValuesSensor = new HashMap<>();
datasAlertas = new ArrayList<>();
runOnUiThread(() -> {
int i = 0;
for( Document d : response.getResource().getItems()){
if(response.getResource().getItems().get(i).get("deviceId").equals("Box2")) {
Object alert = response.getResource().getItems().get(i).get("alert");
Object valueSensor = response.getResource().getItems().get(i).get("value");
Object datavalor = response.getResource().getItems().get(i).get("data");
i++;
if(listaValuesSensor.isEmpty()){
listaValues.add(Integer.valueOf(valueSensor.toString()));
listaValuesSensor.put(alert.toString(),listaValues );
datasAlertas.add(datavalor.toString());
}else{
if(listaValuesSensor.containsKey(alert.toString())){
ArrayList<Integer> o = listaValuesSensor.get(alert.toString());
o.add(Integer.valueOf(valueSensor.toString()));
listaValuesSensor.put(alert.toString(),o );
datasAlertas.add(datavalor.toString());
}else{
listaValues = new ArrayList<>();
listaValues.add(Integer.valueOf(valueSensor.toString()));
listaValuesSensor.put(alert.toString(), listaValues);
datasAlertas.add(datavalor.toString());
}
}
if(!idnomes.contains(alert.toString())) {
d.setId(alert.toString());
idnomes.add(alert.toString());
}
}
dialog.cancel();
}
});
Intent i = new Intent(getApplicationContext(),ValuesActivity.class);
intent.putExtra("id_equipamento", "Box2");
//intent.putExtra("listaValuesSensor", listaValuesSensor.get(coll.getId()).toString());
intent.putExtra("listaValuesSensor", listaValuesSensor);
intent.putExtra("dataValues", datasAlertas);
startActivity(i);
}));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
You didn't pointed out which variables you can't access, so we have to figure it out, but your problem is not that they are in different threads. It seems to be related to the variables' visibility in different scopes. If everything is declared in the same Activity, then you should not have any problems accessing them in the code that you posted, but if they are not in the same class, then you have to change your design.
The only thing I could see is that you are declaring "i" and trying to use "intent":
Intent i = new Intent(getApplicationContext(),ValuesActivity.class);
intent.putExtra("id_equipamento", "Box2");
//intent.putExtra("listaValuesSensor", listaValuesSensor.get(coll.getId()).toString());
intent.putExtra("listaValuesSensor", listaValuesSensor);
intent.putExtra("dataValues", datasAlertas);

Android MapFragment , unable to draw on map & unable to restore previous map state

I'm developing an app that has a app feature for displaying teams on Google maps as markers.
I'm able to display myself as marker that updates when I move & others as markers on a fragment.
The issue is markers are displayed ONLY THE 1st TIME that I go to MapFragment. When I navigate away to another fragment and come back to the map, I see an EMPTY map with no markers & no zoom buttons.
Attempt #3, please see history for previous implementations which are slightly different:
My fragment layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/incident_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
/>
<ProgressBar
android:id="#+id/incident_map_progress_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"/>
<!--<com.google.android.gms.maps.MapView
android:id="#+id/incident_map"
android:layout_width="match_parent"
android:layout_height="match_parent" />-->
</RelativeLayout>
My IncidentMapFragment code, which is now updated based on user feedback. The updates are minimal. Instead on onResume() im using onActivityCreated() and instead of onPause() Im using onSaveInstanceState(), see updated code below
package com.xyz.fragments;
//i did not include imports
//Based on https://developers.google.com/maps/documentation/android-sdk/map-with-marker
public class IncidentMapFragment extends Fragment implements OnMapReadyCallback {
public static final String TAG = "IncidentMapFragment";
private static IncidentMapFragment incidentMapFragment = null;
public static IncidentMapFragment instance() {
if (incidentMapFragment==null)
{
incidentMapFragment = new IncidentMapFragment();
}
return incidentMapFragment;
}
private MapView mapView;
private static GoogleMap map;
private ProgressBar progressBar;
private SupportMapFragment mapFragment;
public static final int UPDATE_MY_CURRENT_LOCATION = 0;
public static final int UPDATE_MY_TEAMS_CURRENT_LOCATIONS = 1;
public static final int UPDATE_ALL_TEAMS_CURRENT_LOCATIONS = 2;
public static final int UPDATE_ALL_LOCATION_BASED_EVENTS = 3;
private static Context context;
private int MY_DEFINED_ACCESS_COARSE_LOCATION_CALLBACK_RESULT = 1;
private int MY_DEFINED_ACCESS_FINE_LOCATION_CALLBACK_RESULT = 2;
boolean flagCoarseLocation = false;
boolean flagFineLocation = false;
private WamsUnitVehicleUnitRelationshipDao vehicleUnitRelationshipDao = new WamsUnitVehicleUnitRelationshipDaoImpl();
private static WamsUnitVehicleUnitRelationship newVehicleUnitRelationship = null;
private static CameraPosition cp;
private static Bundle markersBundle = new Bundle();
private static Bundle saveStateBundle = new Bundle();
private boolean createdStateInDestroyView = false;
private static final String SAVED_BUNDLE_TAG = "IncidentMapFragment_SAVE_STATE";
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.incident_map_fragment, container, false);
context = rootView.getContext();
progressBar = rootView.findViewById(R.id.incident_map_progress_bar);
try {
FragmentManager fm = getActivity().getSupportFragmentManager();
if (savedInstanceState == null) {
mapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.incident_map, mapFragment,TAG).commit();
}
else {
mapFragment = (SupportMapFragment) fm
.findFragmentByTag(TAG);
}
if (savedInstanceState!=null) {
saveStateBundle = savedInstanceState.getBundle(SAVED_BUNDLE_TAG);
//restore camera
cp = saveStateBundle.getParcelable("cp");
//restore bundle of markers
markersBundle = saveStateBundle.getParcelable("markersBundle");
if (cp!=null && markersBundle!=null)
{
reDrawMarkers(markersBundle);
}
}
} catch (Exception exc) {
Log.e(TAG, exc.getMessage());
exc.printStackTrace();
}
return rootView;
}
#Override
public void onActivityCreated(Bundle bundle) {
super.onActivityCreated(bundle);
Log.i(TAG,"onActivityCreated()");
newVehicleUnitRelationship = vehicleUnitRelationshipDao.getWamsUnitVehicleUnitRelationship(NgfrApp.getInstance().getUniqueDeviceId());
if (mapFragment!=null) {
mapFragment.getMapAsync(this);
}
}
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
Log.i(TAG,"onSaveInstanceState()");
outState.putBundle(SAVED_BUNDLE_TAG, saveState());
createdStateInDestroyView = false;
super.onSaveInstanceState(outState);
}
#Override
public void onDestroyView() {
super.onDestroyView();
saveStateBundle = saveState();
createdStateInDestroyView = true;
cp = null;
markersBundle = null;
}
private Bundle saveState() {
Bundle state = new Bundle();
state.putParcelable("cp", cp);
state.putParcelable("markersBundle", markersBundle);
return state;
}
/*Handler used by outside class such as MqttBroker.
1) UPDATE_MY_CURRENT_LOCATION. When my location service send a update location(lat,long) call updateMarkersOnMap() which creates a new AsyncTask to update teh display
2) UPDATE_MY_TEAMS_CURRENT_LOCATIONS. When the MQTT dservice gets new location(lat,long) call updateMarkersOnMap() which creates a new AsyncTask to update teh display
3) UPDATE_ALL_TEAMS_CURRENT_LOCATIONS, not implemented.
*/
public static final Handler updateIncidentMap = new Handler(Looper.getMainLooper()) {
public void handleMessage(Message msg) {
//if the context is null then the MapFragment & GoogleMap objects are NOT instantiated and updating the maps non-existant UI will cause exceptions, NPE, and crashes!
if (context != null) {
Location myCurrentLocation = null;
final int what = msg.what;
switch (what) {
case UPDATE_MY_CURRENT_LOCATION:
Log.i(TAG,"UPDATE_MY_CURRENT_LOCATION");
myCurrentLocation = (Location) msg.obj;
if (map != null) {
updateMarkersOnMap(map,markersBundle, myCurrentLocation.getLatitude(),myCurrentLocation.getLongitude(),newVehicleUnitRelationship.getWamsId(), newVehicleUnitRelationship.getVehicleUnitId());
}
break;
case UPDATE_MY_TEAMS_CURRENT_LOCATIONS:
Log.i(TAG,"UPDATE_MY_TEAMS_CURRENT_LOCATIONS");
if (map != null) {
WamsLocationMarker wamsLocationMarker = (WamsLocationMarker) msg.obj;
updateMarkersOnMap(map, markersBundle,wamsLocationMarker.getLat(),wamsLocationMarker.getLon(), wamsLocationMarker.getClientId(), wamsLocationMarker.getVehicleId());
//mock team
mockTeam(map,markersBundle, wamsLocationMarker.getLat(),wamsLocationMarker.getLon());
}
break;
case UPDATE_ALL_TEAMS_CURRENT_LOCATIONS:
break;
default:
break;
}
} //end if context is NOt null
} //end handleMessage
};
/*I added the #SuppressLint("MissingPermission") because we are handling this in permissionHelper(getActivity()),
and the IDE is too naive to know*/
#SuppressLint("MissingPermission")
#Override
public void onMapReady(GoogleMap googleMap) {
Log.i(TAG, "onMapReady()");
try {
//remove progress bar
progressBar.setVisibility(GONE);
//initially zoom in my location
map = googleMap;
if (permissionHelper(getActivity()))
{
map.setMyLocationEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
map.getUiSettings().setCompassEnabled(true);
}
if (cp != null) {
map.moveCamera(CameraUpdateFactory.newCameraPosition(cp));
cp = null;
}
reDrawMarkers(markersBundle);
}
catch (NullPointerException exc)
{
exc.printStackTrace();
}
}
private static void updateMarkersOnMap(GoogleMap map,Bundle bundle, double lat,double log, String id, String vehicleId) {
final GoogleMap _map = map;
final double _lat = lat;
final double _log = log;
final String _id = id;
final String _vehicleId = vehicleId;
new AsyncTask < Void, Void, WamsLocationMarker > () {
boolean update = false;
#Override
protected WamsLocationMarker doInBackground(Void...voids) {
Marker marker = null;
WamsLocationMarker wamsLocationMarker=null;
try {
Log.i(TAG,"async map: "+map);
if (_map != null && bundle!=null)
{
Log.i(TAG,_map.toString());
//if the wamsLocationMarker exists, then UPDATE
if (bundle.containsKey(_id)) {
Log.i(TAG,"markersBundle.containsKey(_id): "+ bundle.containsKey(_id));
Log.i(TAG,"update true");
//get from hashmap
wamsLocationMarker = (WamsLocationMarker)bundle.get(_id);
update = true;
} else {
//add to map
//if the ids are equal then this is YOU
wamsLocationMarker = new WamsLocationMarkerFactory().createWamsLocationMarker(_id, _vehicleId, _lat, _log);
Log.i(TAG,"WamsLocationMarker");
Log.i(TAG,"update false");
}
} else {
Log.e(TAG, " updateMarkersOnMap() map is " + _map);
}
}
catch (NullPointerException exc)
{
exc.printStackTrace();
}
return wamsLocationMarker;
}
#Override
protected void onPostExecute(WamsLocationMarker wamsLocationMarker) {
super.onPostExecute(wamsLocationMarker);
try {
if (wamsLocationMarker != null) {
Log.i(TAG,"onPostExecute() update:"+update+",wamsLocationMarker:"+wamsLocationMarker);
if (update) {
Log.i(TAG,"onPostExecute() update:"+update);
//UPDATE wth new lat & long if the markers coordinates have change, else dont redraw, beacuse the draw causes a refresh affect & its also a waste computationally
if (wamsLocationMarker.getMarker().getPosition().latitude != _lat && wamsLocationMarker.getMarker().getPosition().longitude != _log) {
LatLng latLng = new LatLng(_lat, _log);
//UPDATE THE MARKER POSITION
wamsLocationMarker.getMarker().setPosition(latLng);
}
} else {
//ADD A NEW MARKER
Marker marker = _map.addMarker(wamsLocationMarker.getMakerOptions());
Log.i(TAG,"ASYNC MARKER:"+marker.getId());
wamsLocationMarker.setMarker(marker);
markersBundle.remove(wamsLocationMarker.getClientId());
markersBundle.putParcelable(wamsLocationMarker.getClientId(), wamsLocationMarker);
}
}
}
catch (NullPointerException exc)
{
exc.printStackTrace();
}
}
}.execute();
}
public void reDrawMarkers(Bundle bundle) {
Log.i(TAG,"reDrawMarkers()");
if (bundle!=null) {
Set<String> keys = bundle.keySet();
WamsLocationMarker wamsLocationMarker = null;
for (String k : keys) {
Log.i(TAG, "key:" + k);
wamsLocationMarker = (WamsLocationMarker) bundle.getParcelable(k);
updateMarkersOnMap(map, bundle, wamsLocationMarker.getLat(), wamsLocationMarker.getLon(), wamsLocationMarker.getClientId(), wamsLocationMarker.getVehicleId());
}
}
}
//Just for test - Im mocking some there points to represent other teams mates on the map, this is just for testing
private static void mockTeam(GoogleMap map, Bundle bundle, double _lat, double _log) {
updateMarkersOnMap(map,bundle, _lat+0.001, _log , "mock111111111", newVehicleUnitRelationship.getVehicleUnitId());
updateMarkersOnMap(map,bundle,_lat, _log+0.001 ,"mock222222222", newVehicleUnitRelationship.getVehicleUnitId());
updateMarkersOnMap(map,bundle, _lat-0.001, _log,"mock33333333", newVehicleUnitRelationship.getVehicleUnitId());
updateMarkersOnMap(map,bundle, _lat, _log-0.001,"mock444444444", newVehicleUnitRelationship.getVehicleUnitId());
}
//Ask the user if required & attempt to grant required location services
private boolean permissionHelper(Activity activity) {
String permission = Manifest.permission.ACCESS_COARSE_LOCATION;
int res = getContext().checkCallingPermission(permission);
//if the required coarse & fine permissions are granted then return true else proceed to get the permissions
if (res == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "ACCESS_COARSE_LOCATION GRANTED");
permission = Manifest.permission.ACCESS_FINE_LOCATION;
res = getContext().checkCallingPermission(permission);
if (res == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "ACCESS_FINE_LOCATION GRANTED");
flagFineLocation = true;
} else {
Log.i(TAG, "ACCESS_FINE_LOCATION NOT GRANTED");
}
flagCoarseLocation = true;
}
//prompt user for COARSE location permission. If the user cancel then display toast & navigate back
if (!flagCoarseLocation) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.ACCESS_COARSE_LOCATION)) {
dialogForCoarsePermission();
} else {
ActivityCompat.requestPermissions(activity,
new String[] {
Manifest.permission.ACCESS_COARSE_LOCATION
},
MY_DEFINED_ACCESS_COARSE_LOCATION_CALLBACK_RESULT);
flagCoarseLocation = true;
}
}
//prompt user for FINE location permission. If the user cancel then display toast & navigate back
if (!flagFineLocation) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.ACCESS_FINE_LOCATION)) {
dialogForFinePermission();
} else {
ActivityCompat.requestPermissions(activity,
new String[] {
Manifest.permission.ACCESS_COARSE_LOCATION
},
MY_DEFINED_ACCESS_FINE_LOCATION_CALLBACK_RESULT);
flagFineLocation = true;
}
}
if (!flagCoarseLocation)
{
Log.i(TAG, "ACCESS_COARSE_LOCATION NOT GRANTED");
}
else
{
Log.i(TAG, "ACCESS_COARSE_LOCATION GRANTED");
}
if (!flagFineLocation)
{
Log.i(TAG, "ACCESS_FINE_LOCATION NOT GRANTED");
}
else
{
Log.i(TAG, "ACCESS_FINE_LOCATION GRANTED");
}
return (flagCoarseLocation && flagFineLocation);
}
private void dialogForCoarsePermission() {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
ActivityCompat.requestPermissions(getActivity(),
new String[] {
Manifest.permission.ACCESS_COARSE_LOCATION
},
MY_DEFINED_ACCESS_COARSE_LOCATION_CALLBACK_RESULT);
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("The map requires coarse location permission.Please it by pressing Yes. ").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
private void dialogForFinePermission() {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
ActivityCompat.requestPermissions(getActivity(),
new String[] {
Manifest.permission.ACCESS_FINE_LOCATION
},
MY_DEFINED_ACCESS_FINE_LOCATION_CALLBACK_RESULT);
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("The map ALSO requires fine location permission.Please it by pressing Yes. ").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
Log.i(TAG, "onRequestPermissionsResult() request code:" + requestCode); // Log printed
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_DEFINED_ACCESS_COARSE_LOCATION_CALLBACK_RESULT) {
flagCoarseLocation = true;
Toast.makeText(context, "Coarse location permission granted.", Toast.LENGTH_SHORT).show();
}
if (requestCode == MY_DEFINED_ACCESS_FINE_LOCATION_CALLBACK_RESULT) {
flagFineLocation = true;
Toast.makeText(context, "Fine location permission granted.", Toast.LENGTH_SHORT).show();
}
}
}
I provided the GUI & corresponding logs for this fragment only.
The 1st time I navigate to the fragment. Everything is ok, and you see the go to my location button & zoom buttons:
Now the issue appears when I navigate to 1st fragment called "Services" or if leave app in the background for a while. I cant see the markers anymore & new location updates coming via the network are added however are NOT being drawn. Map zoom buttons are also missing!!!
See snapshot & logs below:
08-16 08:06:03.358 1314-1314/com.xyz I/IncidentMapFragment: onSaveInstanceState()
08-16 08:06:03.836 1314-1314/com.xyz I/IncidentMapFragment: reDrawMarkers()
key:015140000100161
key:mock33333333
key:mock444444444
key:mock111111111
key:mock222222222
08-16 08:06:03.836 1314-1340/com.xyz I/IncidentMapFragment: async map: com.google.android.gms.maps.GoogleMap#1e72e3b
com.google.android.gms.maps.GoogleMap#1e72e3b
markersBundle.containsKey(_id): true
update true
async map: com.google.android.gms.maps.GoogleMap#1e72e3b
com.google.android.gms.maps.GoogleMap#1e72e3b
markersBundle.containsKey(_id): true
update true
async map: com.google.android.gms.maps.GoogleMap#1e72e3b
com.google.android.gms.maps.GoogleMap#1e72e3b
markersBundle.containsKey(_id): true
update true
async map: com.google.android.gms.maps.GoogleMap#1e72e3b
com.google.android.gms.maps.GoogleMap#1e72e3b
markersBundle.containsKey(_id): true
update true
async map: com.google.android.gms.maps.GoogleMap#1e72e3b
com.google.android.gms.maps.GoogleMap#1e72e3b
markersBundle.containsKey(_id): true
update true
08-16 08:06:03.837 1314-1314/com.xyz I/IncidentMapFragment: onActivityCreated()
08-16 08:06:03.851 1314-1314/com.xyz I/IncidentMapFragment: onPostExecute() update:true,wamsLocationMarker:com.xyz.mapping.WamsLocationMarker#19b04df
onPostExecute() update:true
onPostExecute() update:true,wamsLocationMarker:com.xyz.mapping.WamsLocationMarker#47c13e2
onPostExecute() update:true
onPostExecute() update:true,wamsLocationMarker:com.xyz.mapping.WamsLocationMarker#6b22ea9
onPostExecute() update:true
onPostExecute() update:true,wamsLocationMarker:com.xyz.mapping.WamsLocationMarker#e387818
08-16 08:06:03.852 1314-1314/com.xyz I/IncidentMapFragment: onPostExecute() update:true
onPostExecute() update:true,wamsLocationMarker:com.xyz.mapping.WamsLocationMarker#dab0d7
onPostExecute() update:true
onMapReady()
ACCESS_COARSE_LOCATION GRANTED
ACCESS_FINE_LOCATION GRANTED
08-16 08:06:03.853 1314-1314/com.xyz I/IncidentMapFragment: reDrawMarkers()
08-16 08:06:03.854 1314-1314/com.xyz I/IncidentMapFragment: key:015140000100161
key:mock33333333
key:mock444444444
key:mock111111111
key:mock222222222
08-16 08:06:03.854 1314-1338/com.xyz I/IncidentMapFragment: async map: com.google.android.gms.maps.GoogleMap#b7d1933
com.google.android.gms.maps.GoogleMap#b7d1933
markersBundle.containsKey(_id): true
update true
async map: com.google.android.gms.maps.GoogleMap#b7d1933
com.google.android.gms.maps.GoogleMap#b7d1933
markersBundle.containsKey(_id): true
update true
async map: com.google.android.gms.maps.GoogleMap#b7d1933
com.google.android.gms.maps.GoogleMap#b7d1933
markersBundle.containsKey(_id): true
update true
async map: com.google.android.gms.maps.GoogleMap#b7d1933
com.google.android.gms.maps.GoogleMap#b7d1933
markersBundle.containsKey(_id): true
update true
async map: com.google.android.gms.maps.GoogleMap#b7d1933
com.google.android.gms.maps.GoogleMap#b7d1933
markersBundle.containsKey(_id): true
update true
08-16 08:06:03.865 1314-1314/com.xyz I/IncidentMapFragment: onPostExecute() update:true,wamsLocationMarker:com.xyz.mapping.WamsLocationMarker#19b04df
onPostExecute() update:true
onPostExecute() update:true,wamsLocationMarker:com.xyz.mapping.WamsLocationMarker#47c13e2
onPostExecute() update:true
onPostExecute() update:true,wamsLocationMarker:com.xyz.mapping.WamsLocationMarker#6b22ea9
onPostExecute() update:true
onPostExecute() update:true,wamsLocationMarker:com.xyz.mapping.WamsLocationMarker#e387818
onPostExecute() update:true
onPostExecute() update:true,wamsLocationMarker:com.xyz.mapping.WamsLocationMarker#dab0d7
onPostExecute() update:true
See debug window: state is restored but not drawing
My logs show the markers are being added again with no errors or exceptions, but not being displayed?** Why? And how can i fix this?
Thanks
Obviously the 1st fragment is calling onResume(),onMapReady(),onPause(), which is resetting the map
You are probably handling it wrongly therefore I still assume the problem is still about the Activity /Fragment State Change.
One easy tweak to see whether it is due to the fragment state change is to stoer all the values to the SharedPreference whenever a user place a marker, and assign them to the map every time the state is change (onPause, onResume etc).
For Fragment State Change Explanation
Different events, some user-triggered and some system-triggered, can cause an Activity / Fragment to transition from one state to another as well as resetting the activity/fragment. Therefore, in android, you need to handle different state of your activity as well as fragment.
For handling activity state change, you could reference Android doc.
For your case:
There is a really really good example and details on how to handle Fragment State Change.
I hope it helps!
You don't have to store state changes for this as other answer suggested. But the MapFragment implementation is wrong. You have to fix it.
Change the code in onActivityCreated() to this and move it to onCreate(). You don't have to override onActivityCreated at all.
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.incident_map);
mapFragment.getMapAsync(this);
Assuming the code you gave is correct, replace your code with this. Try it out, I have make some changes
package com.xyz.fragments;
//I didnt not include imports in order to have a smaller post on Stackoverflow
//Based on https://developers.google.com/maps/documentation/android-sdk/map-with-marker
public class IncidentMapFragment extends Fragment implements OnMapReadyCallback {
public static final String TAG = "IncidentMapFragment";
private static IncidentMapFragment incidentMapFragment = null;
public static IncidentMapFragment instance() {
if (incidentMapFragment==null)
{
incidentMapFragment = new IncidentMapFragment();
}
return incidentMapFragment;
}
private MapView mapView;
private static GoogleMap map;
private ProgressBar progressBar;
private SupportMapFragment mapFragment;
public static final int UPDATE_MY_CURRENT_LOCATION = 0;
public static final int UPDATE_MY_TEAMS_CURRENT_LOCATIONS = 1;
public static final int UPDATE_ALL_TEAMS_CURRENT_LOCATIONS = 2;
public static final int UPDATE_ALL_LOCATION_BASED_EVENTS = 3;
private static Context context;
private int MY_DEFINED_ACCESS_COARSE_LOCATION_CALLBACK_RESULT = 1;
private int MY_DEFINED_ACCESS_FINE_LOCATION_CALLBACK_RESULT = 2;
boolean flagCoarseLocation = false;
boolean flagFineLocation = false;
private static CameraPosition cp;
private WamsUnitVehicleUnitRelationshipDao vehicleUnitRelationshipDao = new WamsUnitVehicleUnitRelationshipDaoImpl();
private static WamsUnitVehicleUnitRelationship newVehicleUnitRelationship = null;
private static Bundle bundleOfMarkers = new Bundle();
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.incident_map_fragment, container, false);
context = rootView.getContext();
Bundle bundleOfMarkers = new Bundle();
progressBar = rootView.findViewById(R.id.incident_map_progress_bar);
try {
FragmentManager fm = getActivity().getSupportFragmentManager();
mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.incident_map);
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.incident_map, mapFragment).commit();
}
} catch (Exception exc) {
Log.e(TAG, exc.getMessage());
exc.printStackTrace();
}
return rootView;
}
#Override
public void onActivityCreated(Bundle bundle) {
super.onActivityCreated(bundle);
Log.i(TAG,"onActivityCreated()");
newVehicleUnitRelationship = vehicleUnitRelationshipDao.getWamsUnitVehicleUnitRelationship(NgfrApp.getInstance().getUniqueDeviceId());
if (bundle!=null) {
bundleOfMarkers = bundle.getBundle("bundleOfMarkers");
cp = bundle.getParcelable("cp");
}
if (mapFragment!=null) {
mapFragment.getMapAsync(this);
}
}
/*Handler used by outside class such as MqttBroker.
1) UPDATE_MY_CURRENT_LOCATION. When my location service send a update location(lat,long) call updateMarkersOnMap() which creates a new AsyncTask to update teh display
2) UPDATE_MY_TEAMS_CURRENT_LOCATIONS. When the MQTT dservice gets new location(lat,long) call updateMarkersOnMap() which creates a new AsyncTask to update teh display
3) UPDATE_ALL_TEAMS_CURRENT_LOCATIONS, not implemented.
*/
public static final Handler updateIncidentMap = new Handler(Looper.getMainLooper()) {
public void handleMessage(Message msg) {
//if the context is null then the MapFragment & GoogleMap objects are NOT instantiated and updating the maps non-existant UI will cause exceptions, NPE, and crashes!
if (context != null) {
Location myCurrentLocation = null;
final int what = msg.what;
switch (what) {
case UPDATE_MY_CURRENT_LOCATION:
Log.i(TAG,"UPDATE_MY_CURRENT_LOCATION");
myCurrentLocation = (Location) msg.obj;
if (map != null) {
updateMarkersOnMap(map, myCurrentLocation.getLatitude(),myCurrentLocation.getLongitude(),newVehicleUnitRelationship.getWamsId(), newVehicleUnitRelationship.getVehicleUnitId());
}
break;
case UPDATE_MY_TEAMS_CURRENT_LOCATIONS:
Log.i(TAG,"UPDATE_MY_TEAMS_CURRENT_LOCATIONS");
if (map != null) {
WamsLocationMarker wamsLocationMarker = (WamsLocationMarker) msg.obj;
updateMarkersOnMap(map, wamsLocationMarker.getLat(),wamsLocationMarker.getLon(), wamsLocationMarker.getClientId(), wamsLocationMarker.getVehicleId());
//mock team
mockTeam(map, wamsLocationMarker.getLat(),wamsLocationMarker.getLon());
}
break;
case UPDATE_ALL_TEAMS_CURRENT_LOCATIONS:
break;
default:
break;
}
} //end if context is NOt null
} //end handleMessage
};
/*I added the #SuppressLint("MissingPermission") because we are handling this in permissionHelper(getActivity()),
and the IDE is too naive to know*/
#SuppressLint("MissingPermission")
#Override
public void onMapReady(GoogleMap googleMap) {
Log.i(TAG, "onMapReady()");
try {
//remove progress bar
progressBar.setVisibility(GONE);
//initially zoom in my location
map = googleMap;
if (permissionHelper(getActivity()))
{
map.setMyLocationEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
map.getUiSettings().setCompassEnabled(true);
}
if (cp != null) {
map.moveCamera(CameraUpdateFactory.newCameraPosition(cp));
cp = null;
}
myResume();
}
catch (NullPointerException exc)
{
exc.printStackTrace();
}
}
private static void updateMarkersOnMap(GoogleMap map,double lat,double log, String id, String vehicleId) {
final GoogleMap _map = map;
final double _lat = lat;
final double _log = log;
final String _id = id;
final String _vehicleId = vehicleId;
new AsyncTask < Void, Void, WamsLocationMarker > () {
boolean update = false;
#Override
protected WamsLocationMarker doInBackground(Void...voids) {
Marker marker = null;
WamsLocationMarker wamsLocationMarker=null;
try {
Log.i(TAG,"async map: "+map);
if (_map != null)
{
Log.i(TAG,_map.toString());
//if the wamsLocationMarker exists, then UPDATE
if (bundleOfMarkers.containsKey(_id)) {
Log.i(TAG,"bundleOfMarkers.containsKey(_id): "+bundleOfMarkers.containsKey(_id));
Log.i(TAG,"update true");
//get from hashmap
wamsLocationMarker = (WamsLocationMarker)bundleOfMarkers.get(_id);
update = true;
} else {
//add to map
//if the ids are equal then this is YOU
wamsLocationMarker = new WamsLocationMarkerFactory().createWamsLocationMarker(_id, _vehicleId, _lat, _log);
Log.i(TAG,"WamsLocationMarker");
Log.i(TAG,"update false");
}
} else {
Log.e(TAG, " updateMarkersOnMap() map is " + _map);
}
}
catch (NullPointerException exc)
{
exc.printStackTrace();
}
return wamsLocationMarker;
}
#Override
protected void onPostExecute(WamsLocationMarker wamsLocationMarker) {
super.onPostExecute(wamsLocationMarker);
try {
if (wamsLocationMarker != null) {
Log.i(TAG,"onPostExecute() update:"+update+",wamsLocationMarker:"+wamsLocationMarker);
if (update) {
Log.i(TAG,"onPostExecute() update:"+update);
//UPDATE wth new lat & long if the markers coordinates have change, else dont redraw, beacuse the draw causes a refresh affect & its also a waste computationally
if (wamsLocationMarker.getMarker().getPosition().latitude != _lat && wamsLocationMarker.getMarker().getPosition().longitude != _log) {
LatLng latLng = new LatLng(_lat, _log);
//UPDATE THE MARKER POSITION
wamsLocationMarker.getMarker().setPosition(latLng);
}
} else {
//ADD A NEW MARKER
Marker marker = _map.addMarker(wamsLocationMarker.getMakerOptions());
Log.i(TAG,"ASYNC MARKER:"+marker.getId());
wamsLocationMarker.setMarker(marker);
bundleOfMarkers.remove(wamsLocationMarker.getClientId());
bundleOfMarkers.putParcelable(wamsLocationMarker.getClientId(), wamsLocationMarker);
}
}
}
catch (NullPointerException exc)
{
exc.printStackTrace();
}
}
}.execute();
}
/*I also tried using onSaveInstanceState & onViewStateRestored. I saved the markers on map in Bundle & put them in parceable.
However I was unable to redraw the map*/
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG,"onSaveInstanceState()");
myPause();
outState.putBundle("bundleOfMarkers", bundleOfMarkers);
outState.putParcelable("cp",cp);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.i(TAG,"onRestoreInstanceState()");
super.onRestoreInstanceState(savedInstanceState);
bundleOfMarkers = savedInstanceState.getBundle("bundleOfMarkers");
myResume();
}
/*
#Override
public void onViewStateRestored(#Nullable Bundle savedInstanceState) {
Log.i(TAG,"onViewStateRestored(),savedInstanceState:"+savedInstanceState);
super.onViewStateRestored(savedInstanceState);
}*/
/* #Override
public void onResume() {
super.onResume();
Log.i(TAG,"onResume()");
if (mapFragment!=null) {
mapFragment.getMapAsync(this);
}
newVehicleUnitRelationship = vehicleUnitRelationshipDao.getWamsUnitVehicleUnitRelationship(NgfrApp.getInstance().getUniqueDeviceId());
}*/
/*#Override
public void onPause() {
Log.i(TAG,"onPause()");
super.onPause();
myPause();
}*/
public void myResume() {
Log.i(TAG,"myResume()");
Set<String> keys = bundleOfMarkers.keySet();
WamsLocationMarker wamsLocationMarker = null;
for (String k : keys) {
Log.i(TAG,"key:"+k);
wamsLocationMarker = (WamsLocationMarker) bundleOfMarkers.getParcelable(k);
updateMarkersOnMap(map, wamsLocationMarker.getLat(), wamsLocationMarker.getLon(), wamsLocationMarker.getClientId(), wamsLocationMarker.getVehicleId());
}
}
public void myPause()
{
Log.i(TAG,"myPause()");
if (map!=null) {
//lets get the camera position & markers
cp = map.getCameraPosition();
}
//keys in hashmap
Set<String> keys = IncidentMapFragment.bundleOfMarkers.keySet();
WamsLocationMarker wamsLocationMarker = null;
for (String k : keys) {
wamsLocationMarker = (WamsLocationMarker) IncidentMapFragment.bundleOfMarkers.get(k);
bundleOfMarkers.putParcelable(k,wamsLocationMarker);
}
}
//Just for test - Im mocking some there points to represent other teams mates on the map, this is just for testing
private static void mockTeam(GoogleMap map, double _lat, double _log) {
updateMarkersOnMap(map, _lat+0.001, _log , "mock111111111", newVehicleUnitRelationship.getVehicleUnitId());
updateMarkersOnMap(map,_lat, _log+0.001 ,"mock222222222", newVehicleUnitRelationship.getVehicleUnitId());
updateMarkersOnMap(map, _lat-0.001, _log,"mock33333333", newVehicleUnitRelationship.getVehicleUnitId());
updateMarkersOnMap(map, _lat, _log-0.001,"mock444444444", newVehicleUnitRelationship.getVehicleUnitId());
}
//Ask the user if required & attempt to grant required location services
private boolean permissionHelper(Activity activity) {
String permission = Manifest.permission.ACCESS_COARSE_LOCATION;
int res = getContext().checkCallingPermission(permission);
//if the required coarse & fine permissions are granted then return true else proceed to get the permissions
if (res == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "ACCESS_COARSE_LOCATION GRANTED");
permission = Manifest.permission.ACCESS_FINE_LOCATION;
res = getContext().checkCallingPermission(permission);
if (res == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "ACCESS_FINE_LOCATION GRANTED");
flagFineLocation = true;
} else {
Log.i(TAG, "ACCESS_FINE_LOCATION NOT GRANTED");
}
flagCoarseLocation = true;
}
//prompt user for COARSE location permission. If the user cancel then display toast & navigate back
if (!flagCoarseLocation) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.ACCESS_COARSE_LOCATION)) {
dialogForCoarsePermission();
} else {
ActivityCompat.requestPermissions(activity,
new String[] {
Manifest.permission.ACCESS_COARSE_LOCATION
},
MY_DEFINED_ACCESS_COARSE_LOCATION_CALLBACK_RESULT);
flagCoarseLocation = true;
}
}
//prompt user for FINE location permission. If the user cancel then display toast & navigate back
if (!flagFineLocation) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.ACCESS_FINE_LOCATION)) {
dialogForFinePermission();
} else {
ActivityCompat.requestPermissions(activity,
new String[] {
Manifest.permission.ACCESS_COARSE_LOCATION
},
MY_DEFINED_ACCESS_FINE_LOCATION_CALLBACK_RESULT);
flagFineLocation = true;
}
}
if (!flagCoarseLocation)
{
Log.i(TAG, "ACCESS_COARSE_LOCATION NOT GRANTED");
}
else
{
Log.i(TAG, "ACCESS_COARSE_LOCATION GRANTED");
}
if (!flagFineLocation)
{
Log.i(TAG, "ACCESS_FINE_LOCATION NOT GRANTED");
}
else
{
Log.i(TAG, "ACCESS_FINE_LOCATION GRANTED");
}
return (flagCoarseLocation && flagFineLocation);
}
private void dialogForCoarsePermission() {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
ActivityCompat.requestPermissions(getActivity(),
new String[] {
Manifest.permission.ACCESS_COARSE_LOCATION
},
MY_DEFINED_ACCESS_COARSE_LOCATION_CALLBACK_RESULT);
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("The map requires coarse location permission.Please it by pressing Yes. ").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
private void dialogForFinePermission() {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
ActivityCompat.requestPermissions(getActivity(),
new String[] {
Manifest.permission.ACCESS_FINE_LOCATION
},
MY_DEFINED_ACCESS_FINE_LOCATION_CALLBACK_RESULT);
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("The map ALSO requires fine location permission.Please it by pressing Yes. ").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
Log.i(TAG, "onRequestPermissionsResult() request code:" + requestCode); // Log printed
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_DEFINED_ACCESS_COARSE_LOCATION_CALLBACK_RESULT) {
flagCoarseLocation = true;
Toast.makeText(context, "Coarse location permission granted.", Toast.LENGTH_SHORT).show();
}
if (requestCode == MY_DEFINED_ACCESS_FINE_LOCATION_CALLBACK_RESULT) {
flagFineLocation = true;
Toast.makeText(context, "Fine location permission granted.", Toast.LENGTH_SHORT).show();
}
}
}
**Fragment Adapter class:**
package com.xyz.views;
//didnt include imports
public class MainActivityViewPagerAdapter extends FragmentStatePagerAdapter {
public MainActivityViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment returnFragment=null;
switch(position) {
case 0:
returnFragment = ServicesFragment.newInstance();
break;
case 1:
returnFragment = BiometricsFragment.newInstance();
break;
case 2:
returnFragment =IncidentMapFragment.newInstance();
break;
}
return returnFragment;
}
#Override
public int getCount() {
return 3;
}
public CharSequence getPageTitle(int position) {
CharSequence title=null;
switch (position) {
case 0:
title = "Services";
break;
case 1:
title = "Biometrics";
break;
case 2:
title = "Incident Map";
break;
}
return title;
}
}

Rerouting in Here Map Android SDK

I am using Here Map Android SDK for navigation functionalities. Currently my app can calculate the route and draw it through NavigationManager. But the rerouting function can't work even I have added the RerouteListener to NavigationManager. My code is as following:
public class Navigation extends ActionBarActivity {
private GeoCoordinate destinationGeo;
private GeoCoordinate originGeo;
private static Map mMap = null;
private static MapFragment mMapFragment = null;
private static MapRoute mMapRoute = null;
private static RouteManager mRouteManager = null;
private static PositioningManager mPositoningManager = null;
private static NavigationManager mNavigationManager = null;
private static boolean mPositioningListenerPaused = true;
RouteCalculationTask mRouteCalculationTask;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
/*
//get destination geo passed from CarParkingActivity
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String lat = extras.getString("lat");
String lng = extras.getString("lng");
*/
//destinationGeo = new GeoCoordinate(Double.parseDouble(lat), Double.parseDouble(lng));
destinationGeo = new GeoCoordinate(1.37374, 103.9707);
mMapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.navigation_nokia);
if (mMapFragment == null){
System.out.println("mapfragment null");
}
mMapFragment.init(new OnEngineInitListener() {
#Override
public void onEngineInitializationCompleted(Error error) {
if (error == Error.NONE){
System.out.println("map engine init no error");
mMap = mMapFragment.getMap();
mMapFragment.getView().setVisibility(View.INVISIBLE);
mMap.setZoomLevel(mMap.getMaxZoomLevel());
mMap.setCenter(destinationGeo, Map.Animation.NONE);
//set destination marker on map
Image image = new Image();
try{
image.setImageResource(R.drawable.carpark4);
}
catch (IOException e){
e.printStackTrace();
}
MapMarker mapMarker = new MapMarker(destinationGeo, image);
mMap.addMapObject(mapMarker);
//set navigation marker on map
try{
image.setImageResource(R.drawable.gnavigation);
}catch (IOException e){
e.printStackTrace();
}
mMap.getPositionIndicator().setMarker(image);
mMap.getPositionIndicator().setVisible(true);
//set traffic information
mMap.setTrafficInfoVisible(true);
//set map scheme
mMap.setMapScheme(Map.Scheme.CARNAV_DAY);
mRouteManager = RouteManager.getInstance();
mPositoningManager = PositioningManager.getInstance();
mPositoningManager.addListener(new WeakReference<PositioningManager.OnPositionChangedListener>(mPositioningListener));
// start positioning manager
if (mPositoningManager.start(PositioningManager.LocationMethod.GPS_NETWORK)){
MapEngine.getInstance().onResume();
mPositioningListenerPaused = false;
mRouteCalculationTask = new RouteCalculationTask(RouteOptions.Type.FASTEST);
mRouteCalculationTask.execute("hello");
}
mNavigationManager = NavigationManager.getInstance();
mNavigationManager.setMap(mMap);
//set map update mode when movement
mNavigationManager.setMapUpdateMode(NavigationManager.MapUpdateMode.ROADVIEW);
//set up road view in navigation
NavigationManager.RoadView roadView = mNavigationManager.getRoadView();
roadView.addListener(new WeakReference<NavigationManager.RoadView.Listener>(mNavigationManagerRoadViewListener));
roadView.setOrientation(NavigationManager.RoadView.Orientation.DYNAMIC); //heading is at the top of the screen
//set up route recalculation in navigation
mNavigationManager.addRerouteListener(new WeakReference<NavigationManager.RerouteListener>(mNavigaionRerouteListener));
}else {
System.out.println("ERROR: cannot init MapFragment");
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_navigation, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onResume(){
super.onResume();
if (mPositoningManager != null){
// start positioning manager
if (mPositoningManager.start(PositioningManager.LocationMethod.GPS_NETWORK)){
mPositioningListenerPaused = false;
mRouteCalculationTask = new RouteCalculationTask(RouteOptions.Type.FASTEST);
mRouteCalculationTask.execute("hello");
}
}
}
#Override
public void onPause(){
super.onPause();
//stop positioning manager
mPositoningManager.stop();
mPositioningListenerPaused = true;
MapEngine.getInstance().onPause();
}
//RouteManager
private RouteManager.Listener mRouteMangerListener = new RouteManager.Listener() {
#Override
public void onProgress(int i) {
}
#Override
public void onCalculateRouteFinished(RouteManager.Error error, List<RouteResult> list) {
if (error == RouteManager.Error.NONE && list.get(0).getRoute() != null){
//create a map route and place it on the map
Route route = list.get(0).getRoute();
mMapRoute = new MapRoute(route);
mMap.addMapObject(mMapRoute);
//begin navigation
NavigationManager.Error navigationError = mNavigationManager.startNavigation(route);
if (navigationError != NavigationManager.Error.NONE)
{
System.out.println(navigationError);
}
else {
System.out.println("start navigation no error");
mNavigationManager.addNavigationManagerEventListener(new WeakReference< NavigationManager.NavigationManagerEventListener>(mNavigationManagerEventListener));
}
//get boundingbox containing the route and zoom in (no animation)
GeoBoundingBox gbb = route.getBoundingBox();
mMap.zoomTo(gbb, Map.Animation.NONE, Map.MOVE_PRESERVE_ORIENTATION);
}
}
};
private NavigationManager.NavigationManagerEventListener mNavigationManagerEventListener = new NavigationManager.NavigationManagerEventListener() {
#Override
public void onRunningStateChanged() {
super.onRunningStateChanged();
System.out.println("onRunningStateChanged");
}
#Override
public void onNavigationModeChanged(){
super.onNavigationModeChanged();
System.out.println("onNavigationModeChanged");
}
};
private NavigationManager.RoadView.Listener mNavigationManagerRoadViewListener = new NavigationManager.RoadView.Listener() {
#Override
public void onPositionChanged(GeoCoordinate geoCoordinate) {
Log.d("Roadview pos", geoCoordinate.toString());
}
};
private class RouteCalculationTask extends AsyncTask<String, String, String> {
private RouteOptions.Type routeType;
private ProgressDialog progressDialog = new ProgressDialog(Navigation.this);
public RouteCalculationTask(RouteOptions.Type type) {
/*
FASTEST(0),
SHORTEST(1),
ECONOMIC(2);
*/
routeType = type;
}
#Override
protected String doInBackground(String... url){
//clear previous results
if (mMap != null && mMapRoute != null){
mMap.removeMapObject(mMapRoute);
mMapRoute = null;
}
//select routing opitions
RoutePlan routePlan = new RoutePlan();
RouteOptions routeOptions = new RouteOptions();
routeOptions.setTransportMode(RouteOptions.TransportMode.PEDESTRIAN);
routeOptions.setRouteType(routeType);
routePlan.setRouteOptions(routeOptions);
//set start point
for(int i = 0; i < 100; i++)
{
if(mPositoningManager.hasValidPosition())
break;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
originGeo = mPositoningManager.getPosition().getCoordinate();
routePlan.addWaypoint(originGeo);
System.out.println("get originGeo");
//set end point
routePlan.addWaypoint(destinationGeo);
//retrieve routing information via RouteManagerEventListener
RouteManager.Error error = mRouteManager.calculateRoute(routePlan, mRouteMangerListener);
if (error != RouteManager.Error.NONE)
{
/*
NONE(0),
UNKNOWN(1),
OUT_OF_MEMORY(2),
INVALID_PARAMETERS(3),
INVALID_OPERATION(4),
GRAPH_DISCONNECTED(5),
GRAPH_DISCONNECTED_CHECK_OPTIONS(6),
NO_START_POINT(7),
NO_END_POINT(8),
NO_END_POINT_CHECK_OPTIONS(9),
CANNOT_DO_PEDESTRIAN(10),
ROUTING_CANCELLED(11),
VIOLATES_OPTIONS(12),
ROUTE_CORRUPTED(13),
INVALID_CREDENTIALS(14),
REQUEST_TIMEOUT(15),
PT_ROUTING_UNAVAILABLE(16),
OPERATION_NOT_ALLOWED(17),
NO_CONNECTIVITY(18);
*/
System.out.println(error);
}
String data = "";
return data;
}
#Override
protected void onPreExecute(){
super.onPreExecute();
progressDialog.show();
}
#Override
protected void onPostExecute(String result){
super.onPostExecute(result);
mMapFragment.getView().setVisibility(View.VISIBLE);
if (progressDialog.isShowing())
progressDialog.dismiss();
}
}
//define positoning listener
private PositioningManager.OnPositionChangedListener mPositioningListener = new PositioningManager.OnPositionChangedListener() {
#Override
public void onPositionUpdated(PositioningManager.LocationMethod locationMethod, GeoPosition geoPosition, boolean b) {
if (!mPositioningListenerPaused && geoPosition.isValid()){
//originGeo = geoPosition.getCoordinate();
mMap.setCenter(geoPosition.getCoordinate(), Map.Animation.NONE);
Double speed = geoPosition.getSpeed();
Double heading = geoPosition.getHeading();
originGeo = geoPosition.getCoordinate();
System.out.println(originGeo.toString());
System.out.println(speed);
}
}
#Override
public void onPositionFixChanged(PositioningManager.LocationMethod locationMethod, PositioningManager.LocationStatus locationStatus) {
//determine if tunnel extrapolation is active
if (locationMethod == PositioningManager.LocationMethod.GPS){
boolean isExtrapolated = ((mPositoningManager.getRoadElement() != null)
&& (mPositoningManager.getRoadElement().getAttributes().contains(RoadElement.Attribute.TUNNEL)));
boolean hasGPS = (locationStatus == locationStatus.AVAILABLE);
}
}
};
//Route recalculation
private NavigationManager.RerouteListener mNavigaionRerouteListener = new NavigationManager.RerouteListener() {
#Override
public void onRerouteBegin() {
super.onRerouteBegin();
Toast.makeText(getApplicationContext(), "reroute begin", Toast.LENGTH_SHORT).show();
}
#Override
public void onRerouteEnd(Route route){
super.onRerouteEnd(route);
Toast.makeText(getApplicationContext(), "reroute end", Toast.LENGTH_SHORT).show();
}
};
}
from my understanding when your position strays off the calculated route, it should trigger a route (re)calculation. I have tried it and I received both onRerouteBegin and onReRouteEnd callbacks. From my observation staying on route does not trigger any re-routing.

Categories

Resources