How to get the coordinates of the blue dot in android maps v2

99封情书 提交于 2020-01-15 12:33:36

问题


On a Map I want to show the path direction from my location to a certain location. I have implemented the logic for drawing the path on the map already, but I have issues determinate my current location.

I have being using new LocationClient for retrieving my current location just like it is described in this article: http://developer.android.com/training/location/retrieve-current.html , and i have read somewhere that the new maps API v2 is using the same technique for getting my location.

Anyway, when I draw the path directions on the map from my location to the desired one, the starting point of the path is different from the blue dot that marks my current location.

The blue dot shows the right position, and my implementation doesn't. So, is there a way to get the coordinates of the blue dot marker, something like map.getMyLocationMarker().getLocation() or I need to figure out some other way to get my location manually?

UPDATE

I forgot that I have left this question opened, but here is my answer below.


回答1:


Location findme = mMap.getMyLocation();
        double latitude = findme.getLatitude();
        double longitude = findme.getLongitude();
        LatLng latLng = new LatLng(latitude, longitude);

This will give you latitude and longitude of your location.




回答2:


Since googlemyMap.getMyLocation() method is deprecated...

Best simple solution so far that i found

Getting current location and center the camera on marker. Android Studio editor see there is a error but when you run it there is no problem with the code.

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria,false));
if (location != null)
{    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria,false));

        if (location != null)
        {
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));

            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(location.getLatitude(), location.getLongitude()))      // Sets the center of the map to location user
                    .zoom(17)                   // Sets the zoom
                    .build();                   // Creates a CameraPosition from the builder
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
        LatLng goztepe = new LatLng(location.getLatitude(), location.getLongitude());
        mMap.addMarker(new MarkerOptions().position(goztepe).title("Göz Göz GÖZTEPE"));

explained here .




回答3:


The original answer was to use setMyLocationChangeListener() on GoogleMap, to be notified of "blue dot" changes. That is now deprecated.

If you can create a sample project that reproduces your problem, you may wish to file an issue, attaching that project. What you are doing is the official replacement for the deprecated methods, and so one would hope that your code would work properly. If you do file an issue, and if you think of it, post a link to it here, as I'd like to keep an eye on it.




回答4:


It was actually a really stupid mistake I did.

But here is the answer. The procedure described in the link in the question is the right thing to do.

My problem was that I saved the fetched data to a new shared preferences key, and I was reading it from the old one where the wrong location was saved. Yeah it is pretty dumb :)

So follow the tutorial on android developer portal and there will be no mistakes with it.



来源:https://stackoverflow.com/questions/16906585/how-to-get-the-coordinates-of-the-blue-dot-in-android-maps-v2

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