I am using HERE map SDK in my Android app, I get geo location (lat/lon) every 5 seconds. When move the marker from location 1 to location 2, the marker jumps from location 1 to location 2. Does HERE map SDK have an api to move the marker smoothly from location 1 to location 2?
heres how i did it .
before i start tracking the user' location i get his last known location wiht this line of code :
if (isLocationEnabled()) {
LocationThread thread = new LocationThread(this, location ->{
clearPassageiroMapMarker();
passageiro.setLat(location.getLatitude());
passageiro.setLongitude(location.getLongitude());
addPassageiroMarker(new GeoCoordinates(location.getLatitude(),location.getLongitude()),R.drawable.ic_passageiro);
return null;
});
thread.requestLocation();
notice that i add its marker ONCE and only once .. heres the code for that :
public void addPassageiroMarker(GeoCoordinates geoCoordinates, int resourceId) {
MapImage mapImage = MapImageFactory.fromResource(this.getResources(), resourceId);
Anchor2D anchor = new Anchor2D(0.5f, 1.0f);
mapMarker = new MapMarker(geoCoordinates, mapImage, anchor);
mapView.getMapScene().addMapMarker(mapMarker);
mapMarkerPassageiroList.add(mapMarker);
}
notice that i have a mapmarker declared globably ,because im planning on using only one instance of marker throughout the entire instance of the activity...
heres the line of code that updates the marker whenever u get new set of coordinates :
mapMarker.setCoordinates(new GeoCoordinates(location.getLatitude(),location.getLongitude()));
took me a long while to find it out but im glad i did.
however i think if ur loooking for a fancy way of displaying the marker moving like uber has etc, u might have to create an animation of your own.
Related
I'm a beginner in android studio and I'm currently create cycling apps for my final project. in these apps I need to implement Maps and Direction API. I search for tutorials, but they all set the destination. and in my apps the destination is unknown.
it's possible if I display the current location (automatic) and draw a line in Maps while the user is riding, please give a tutorial too? And it's possible if I use a Free API Key for this project?
thank you ...
First, This direction API is not free.
2nd you need to provide start and endpoint for direction API routes.
You can use the current location as your start point and if you have no destination then where do you want to draw a line? Means from your current location to where?
or If you want to draw a tracking line from your starting position to your current position then you don't need Direction API, you just need Live location tracking and can draw Polylines on your map using current location. Following is the code for your help.
To get this library into our app, we need to add the following to our build.gradle file.
implement 'com.google.maps:google-maps-services:0.1.20'
// Initialize Geo Context First
private GeoApiContext getGeoContext() {
GeoApiContext geoApiContext = new GeoApiContext();
return geoApiContext.setQueryRateLimit(3)
.setApiKey(getString(R.string.directionsApiKey))
.setConnectTimeout(1, TimeUnit.SECONDS)
.setReadTimeout(1, TimeUnit.SECONDS)
.setWriteTimeout(1, TimeUnit.SECONDS);
}
// This code will fetch the result from Google direction api from origin to destination
DateTime now = new DateTime();
DirectionsResult result = DirectionsApi.newRequest(getGeoContext()).mode(TravelMode.DRIVING).origin(origin)
.destination(destination)
.departureTime(now).await();
// You can use this method to add marker on the map
private void addMarkersToMap(DirectionsResult results, GoogleMap mMap) {
mMap.addMarker(new MarkerOptions().position(new LatLng(results.routes[0].legs[0].startLocation.lat, results.routes[0].legs[0].startLocation.lng)).title(results.routes[0].legs[0].startAddress));
mMap.addMarker(new MarkerOptions().position(new LatLng(results.routes[0].legs[0].endLocation.lat, results.routes[0].legs[0].endLocation.lng)).title(results.routes[0].legs[0].startAddress).snippet(getEndLocationTitle(results)));
}
// Use this method to draw polyline/routes on your map
private void addPolyline(DirectionsResult results, GoogleMap mMap) {
List<LatLng> decodedPath = PolyUtil.decode(results.routes[0].overviewPolyline.getEncodedPath());
mMap.addPolyline(new PolylineOptions().addAll(decodedPath));
}
I am using Here maps SDK in an android project.
I want to calculate track here and put it into an android map to route exactly this way (calculated on API)
I tried to get points from Json and map them to waypoints, but the calculated way sometimes differs from the original.
What I have tried so far:
1. Get calculateroute.json from
https://route.api.here.com/routing/7.2/calculateroute.json
2. Put coordinates to RoutePlan
RoutePlan plan = new RoutePlan();
plan.addWaypoint(new RouteWaypoint(new GeoCoordinate(54.3782893, 18.7009037, 0), RouteWaypoint.Type.STOP_WAYPOINT));
plan.addWaypoint(new RouteWaypoint(new GeoCoordinate(54.2660919, 18.6583687, 0), RouteWaypoint.Type.STOP_WAYPOINT));
3. Calculate route and put to a map
new CoreRouter().calculateRoute(plan, new CoreRouter.Listener() {
#Override
public void onCalculateRouteFinished(List<RouteResult> list, RoutingError error) {
MapRoute mapRoute = new MapRoute(list.get(0).getRoute()
new Map().addMapObject(mapRoute)
}
}
I want to do :
1. Get calculateroute.json from
https://route.api.here.com/routing/7.2/calculateroute.json
2. Create MapRoute from the calculateroute.json result ( but don't calculate it )
3. Put MapRoute to map and start navigation
Basically there is no direct way to achieve this, a workaround solution is as described in the thread : Backend Calculated Route,Here-API to just show Guidance and route on Map
I have a geojson with some polygons, added it as a source and created a layer to show those features on the mapbox map.
Since I am using this method: (Edit from the sample souce: MapboxGLAndroidSDKTestApp)
final PointF pixel = mapboxMap.getProjection().toScreenLocation(point);
List<Feature> fList = mapboxMap.queryRenderedFeatures(pixel, "polygons");
for(Feature feature:fList){
Log.d(TAG,"FeatureInfo: "+feature.toJson());
}
I can check if the marker is inside one of those polygons by passing the marker position to it.
But when I move my camera far away to the marker, the polygon is not rendered and the checker is not work anymore.
So my question is, is there any way to check if the marker is inside the polygon, which no matter where is my camera pointing at?
To check a point whether lies or not inside a polygon on mapbox
can use turf.js library http://turfjs.org/
This is the way in which you don't need to plot point or polygon on the mapbox(without plotting).
var point = turf.point([-75.343, 39.984]);
// here first is lng and then lat
var polygon = turf.polygon([[
[-2.275543, 53.464547],
[-2.275543, 53.489271],
[-2.215118, 53.489271],
[-2.215118, 53.464547],
[-2.275543, 53.464547]
]], { name: 'poly1'});
// here first is lng and then lat
now you can use inside method from turf lib
i.e turf.inside(point, polygon); // return boolean True/False
Use TurfJoins.inside(yourPoint, yourPolygon)
I am trying to draw the marker showing the user's current location although I have enabled setMycurrentLocation in map while initializing it, so I am getting the the latitude and longitude of current location . Now what I am doing is showing marker at the current location . this is how I am doing it
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 8000, new GoogleMap.CancelableCallback() {
#Override
public void onFinish() {
Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
public void run() {
addMarker(googleMap.getLocation.getLatitude(), googleMap.getLocation.getLatitude());
and my addMarker function goes like this
private void addMarker(Double Lat, Double Long) {
/** Make sure that the map has been initialised **/
if (null != googleMap) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(Lat, Long))
.title("You")
.draggable(true)
);
}
}
So this peace of code of adding marker only works if I try it using out side of onfinished function of map animation , but
some times it gives me a null as sometimes it is unable to get the location from map.
So I tried it to put this exactly after when the camera stop animation.
but in my case it is not working , Any idea ? or what else other way I
can do to add marker when the camera stop animation .?
As from comments and code, it seems you want to have multiple markers of the user location on the map, but the code you are using for obtaining user location is not ok (I can't find googleMap.getLocation and maybe the code is not complete).
The method getMyLocation() of google maps is deprecated:
https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.html#getMyLocation()
This method is deprecated.
use
com.google.android.gms.location.FusedLocationProviderApi instead.
FusedLocationProviderApi provides improved location finding and power
usage and is used by the "My Location" blue dot. See the
MyLocationDemoActivity in the sample applications folder for example
example code, or the Location Developer Guide.
The link points to https://developers.google.com/maps/documentation/android/location
Which then ports you tu use FusedLocationProvider. You have to look at that guides to obtain user location. When you have done it, you can put that code in the onFinish callback.
If you need help for fused location provider maybe you should check for already existent questions or open a new one with the appropriate tags!
I am developing an application which shows the current location of the user. I want to display a marker on the current location of the user.
I have used an image as marker to display current location. But i want to display an animated arrow showing the range just like in the google maps android app. Can anyone please tell me how to achieve this. Any sample code will be of great help...
try this tricky solution...
if u use javascript, try setInterval() to cahnge image if .gif doesn't work.
function runAnimation(callback){
setInterval(function(){
// set new marker here
// var newVal = callback(); <-- use callback to get new value
}
, 500); // interval in millisec
}