Include device location and multiple map markers in camera bounds - android

We're displaying a number of map markers on the map representing various objects which are retrieved either from a cache or from an asynchronous network call. When these objects are retrieved, we update the camera to ensure all markers are within view:
LatLngBounds bounds = mBoundsBuilder.build();
mMapboxMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50, 100, 50, 30));
We're interested in adding the user's location to the map, and have done so using the LocationComponent provided by MapBox as shown in this example.
The problem is that when the LocationComponent has locationComponent .setCameraMode(CameraMode.TRACKING) set as shown, the camera will focus on the user's location, without accommodating the markers previously set. Setting the camera mode to CameraMode.NONE allows the markers to be shown using the code snippet above, however in this case the user's location will not be accommodated in the mBoundsBuilder.
The obvious solution seemed to be to listen for location updates and manually add the coordinates to the mBoundsBuilder so that the user's location is taken into account. This, however, has a number of problems, such as:
Callbacks for location changes can be invoked many times, while the mBoundsBuilder should only contain a single set of coordinates for the user's location, the rest of the objects being the previously added markers.
Location updates are asynchronous, and getLastKnownLocation will often return null.
Questions:
Is there a default, built in way to show a user's location while making sure the camera accommodates map markers?
If no default option exists, how can this be properly implemented?
Version:
com.mapbox.mapboxsdk:mapbox-android-sdk:6.8.1

Take a look at this -> https://www.github.com/bkhezry/MapDrawingTools it helps in drawing polygons on a map.

Related

Making the location indicator in turn-by-turn navigation always look forward even when deviating from the route?

Is it possible to ensure that the location indicator doesn't keep pointing back to the route while you are deviating from it? It makes it very hard to see what's in front of the user as when camera tracking mode is turned on, it locks the camera to the location indicator's direction (e.g indicating to go back -> makes the camera look back),
What I want is to be able point the location indicator to wherever the user is going towards, so if they do a right turn and it just happens to deviate from the route, I want it to continue looking forward and not backwards to the route.
Figured it out.
Since I use a custom location provider, I have to create a custom "HERE Location" object, but there's a nice property in this object to set its bearings; "setBearingInDegrees", simply find the user's bearings and set it using this setter.
e.g:
var bearing = 0.0;
Location location = new Location.Builder()
.setCoordinates(geoCoordinates)
.setBearingInDegrees(bearing)
.build();
You would then set this 'location' back to the VisualNavigator so that it shows this location on the default indicator using its method 'onLocationUpdated'.
Even if you're on camera tracking mode, this will make you face the bearing you set in this location, which is perfect and exactly what I needed.
More information is found in the 'Add a Location Indicator' section:
https://developer.here.com/documentation/android-sdk-navigate/4.10.4.0/dev_guide/topics/map-items.html

Reroute Here-map on destination change. It's a moving target

I am using Here Map Android SDK. Currently my app can calculate the initial route and draw it through NavigationManager. Listeners are implemented and it will recalculate/redraw when the position changes, but I have a moving target: the destination is another vehicle position I get from a web service at 30 second intervals.
What is the proper way to have the route adjusted for a change in destination?
The easy way is to create a new route with an updated destination waypoint, have it calculated, then replace the old route and its listeners with the new one. I fear this wastes computing resources and produces lag/flicker on the map during the redraw. If this is indeed the path to take, how do we minimize screen issues?
I tried just changing the coordinates of the waypoint but it has no impact. I searched for a "route waypoint change" listener, similar to a traffic or position listener but could not find any.
Update: Since Here confirmed a route destination cannot be updated, I clarify my request for "howto":
What objects can I reuse in the new one? Which objects must we remove from the map and/or destroy to avoid leaks?
Initial plan:
keep handle on waypoint, original route (missing any?)
modify the destination waypoint coordinates
create a new route and have it calculated
move the destination map marker to the new destination
add new route
remove the original route (I assume the beginning of the route is similar in most update cases, so we avoid "flicker")
Anything missing? Listeners handling?
Here SDK does not provide such feature as of today. You can not change existing route or update its target. If you have new destination point you should calculate new route.

How do I add markers to a Mapbox map outside of a Mapbox event?

All the examples I've seen for adding a marker to the map show the marker being added in the "onMapReady" callback or the "onMapClick" callback. I have an app that periodically finds the user's latitude and longitude and I want to add a marker to the map when the lat and lng updates. This happens outside of the previously mentioned callbacks. How would I do that? I need access to the Style object of the map but it's only accesible inside the callbacks.
The reason these callbacks (in particular onMapReady) are used in all the examples is to ensure that the mapboxMap objects's style has been loaded by the time you attempt to modify it - if it isn't there is the potential for some asynchronous weirdness as the map renders.
I would recommend you still include the logic for adding the periodic lat/lon coordinates as markers within the onMapReady callback.
⚠️ Disclaimer: I currently work for Mapbox ⚠️

Finding the shortest route from current location to the locations marked on a map

I have mapped my current location and also 4 other locations on my map.Is it possible the mark the route which is closest to the current location(1),then from that(1) to the next closest(2) position,then so on(3).!!Finally i should have the route from my current location marker via all the location in between(markers) till the last marker!!
Thanks in advance!!
You are going to have to use separate instance of DirectionsService object to make your directions request for each destination and then set that to the map using the same instance of the DirectionsRenderer object.
Take a look at this JSFiddle
There are three separate directions in this example. Just make the start the same location.

How to speed up myLocation determination

I am using the new API V2, but that's probably not important since I am newbie for maps anyway:)
I have very simple need. I want to show my location and one target marker and all is working. It's just that the my location determination is not predictable, can get minutes or so to get the blue circle.
What I thought is to manually use the LocationManager to retreive in background the coarse or fine location and pass that value to the Mapactivity. Since normally the map activity will not start immediatelly it would work nice. Then I could also save to databse the last location and pass that in the case the map activity starts before the LocationManager get the real location.
I am looking at the api's but could not find a function that would display imemdiatelly the blue circle at given LatLong. Is this function available?
If this function is not available the only workaround that I could find is to override onlocationchanged to retrieve the new location and save the last, and to display the marker on last known mylocation (that would hide automatically itself on first locationchanged event).
But it would be easier, and also pretier, if I could simply pass initial mylocaltion coordiantes?
Thanks for any idea
I am looking at the api's but could not find a function that would display imemdiatelly the blue circle at given LatLong. Is this function available?
Use setLocationSource() to provide a LocationSource to the GoogleMap. Once your LocationSource is called with activate(), you will be handed a listener object, to which you can pass Location objects for the readings you get from a location provider.

Categories

Resources