如何在android maps v2中获取蓝点的坐标

时间:2013-06-03 22:01:50

标签: android geolocation google-maps-api-2

在地图上我想显示从我的位置到某个位置的路径方向。我已经实现了在地图上绘制路径的逻辑,但是我确定了当前位置的问题。

我正在使用新的LocationClient来检索我当前的位置,就像本文中描述的那样:http://developer.android.com/training/location/retrieve-current.html,我已经读过新地图API v2正在使用相同的技术来获取我的位置。

无论如何,当我在地图上绘制从我的位置到所需位置的路径方向时,路径的起点与标记我当前位置的蓝点不同。

蓝点显示正确的位置,而我的实现则没有。那么,有没有办法获得蓝点标记的坐标,像map.getMyLocationMarker().getLocation()或者我需要找出其他方法来手动获取我的位置?

更新

我忘记了我已经打开了这个问题,但下面是我的答案。

4 个答案:

答案 0 :(得分:3)

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

这将为您提供您所在位置的纬度和经度。

答案 1 :(得分:2)

由于不推荐使用googlemyMap.getMyLocation()方法......

到目前为止我发现的最简单的解决方案

获取当前位置并将相机置于标记上。 Android Studio编辑器看到有错误但是当你运行它时代码没有问题。

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"));

解释here

答案 2 :(得分:0)

最初的答案是使用setMyLocationChangeListener() on GoogleMap,以获得有关“蓝点”更改的通知。现在已弃用。

如果您可以创建一个可以重现问题的示例项目,那么您可能希望file an issue附加该项目。你正在做的是正式替换已弃用的方法,因此人们希望你的代码能正常工作。如果你确实提出了问题,如果你想到了,请在这里发布一个链接,因为我想留意它。

答案 3 :(得分:-1)

这实际上是我犯过的一个非常愚蠢的错误。

但这是答案。问题中的链接中描述的过程是正确的。

我的问题是我将获取的数据保存到新的共享首选项键,我正在从保存错误位置的旧数据中读取它。是的,它非常愚蠢:))

请按照Android开发人员门户网站上的教程进行操作,不会出现任何错误。