Stick position to road on android maps v2

穿精又带淫゛_ 提交于 2019-12-10 19:06:26

问题


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...


回答1:


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.



来源:https://stackoverflow.com/questions/18700915/stick-position-to-road-on-android-maps-v2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!