Stick position to road on android maps v2
Asked Answered
V

1

3

I am developing an app to be used on road while driving, just like Google Maps for Android, the official Google application.

If you set a target and an origin on the google maps app and begin route, it will stick you on road: the arrow marker will be on road, and it kinda looks like a magnet action between your position and the nearest road, so you will never be off-road because you are supposted to be on road.

How is that made? I want to center the map in the user location, but I want it to be "road sticky" and never leaves the road, so I want to have latitude and longitude values on road...

I am using the android maps v2, i've searched arround for a long time but never saw solution to this-

This is my onlocationchanged method:

@Override
public void onLocationChanged(Location location) {

    latitude = location.getLatitude();
    longitude = location.getLongitude();

    debugTv.setText(latitude.toString() + ", " + longitude.toString());

    String msg = "Updated: " + latitude.toString() + ", "
            + longitude.toString();

    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();

    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(latitude, longitude)).zoom(17).tilt(40)
            .build();

    mapa.animateCamera(CameraUpdateFactory
            .newCameraPosition(cameraPosition));

}

Like this, the camera is centering where I am, wich is good, but as the GPS isnt that accurate sometimes, the little marker I have in the center will be off-road sometimes...

Vella answered 9/9, 2013 at 14:45 Comment(0)
P
0

One possible way would be to use Google Directions API.

If you know user's origin and destination, you may make a single request, get all the intermediate points and each time location changes, try to find the nearest point on that route.

If you just want to show user position on the road, you can make multiple requests, but be aware of the limits. See my answer here for some details.

Either way you have to forget default "blue dot" as position indicator and use Marker for that or if you really need to use blue dot, you will have to create proxy LocationSource, which adjusts the position received from LocationClient or LocationManager.

Piloting answered 9/9, 2013 at 18:54 Comment(1)
So that would be the only way? I wish there were a method on the googlemap library to "lock" or "restrict" the user location to be on-road, that would be nice :\Vella

© 2022 - 2024 — McMap. All rights reserved.