【问题标题】:How to get the coordinates of the blue dot in android maps v2如何在android maps v2中获取蓝点的坐标
【发布时间】:2013-05-30 04:26:31
【问题描述】:

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

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

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

蓝点显示正确的位置,而我的实现没有。那么,有没有办法获取蓝点标记的坐标,比如map.getMyLocationMarker().getLocation(),或者我需要想办法手动获取我的位置?

更新

我忘了我没有打开这个问题,但下面是我的答案。

【问题讨论】:

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


    【解决方案1】:
    Location findme = mMap.getMyLocation();
            double latitude = findme.getLatitude();
            double longitude = findme.getLongitude();
            LatLng latLng = new LatLng(latitude, longitude);
    

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

    【讨论】:

    • getMyLocation() 返回空值。调用此方法之前是否有一些先决条件?
    • @djandreski GoogleMap.setMyLocationEnabled 是必填项。
    【解决方案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

    【讨论】:

      【解决方案3】:

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

      如果您可以创建一个重现您的问题的示例项目,您可能希望file an issue 附上该项目。您正在做的是正式替换不推荐使用的方法,因此希望您的代码能够正常工作。如果您确实提出了问题,并且如果您想到了,请在此处发布指向它的链接,因为我想密切关注它。

      【讨论】:

        【解决方案4】:

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

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

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

        所以按照android开发者门户上的教程,就不会出错了。

        【讨论】:

        • 你能给我密码吗? map.getMyLocation() 不适合我
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-24
        • 1970-01-01
        • 2015-01-31
        • 1970-01-01
        • 2013-10-14
        相关资源
        最近更新 更多