【问题标题】:android map adds currentLocation marker away from blue dotandroid地图添加currentLocation标记远离蓝点
【发布时间】:2017-07-27 07:54:56
【问题描述】:

我正在研究谷歌地图 api v2。我有一个按钮,可以将标记添加到我的当前位置。

地图可以正确定位我的位置,但有时(不是每次)当我想在该位置添加标记时,标记会远离蓝点。

我想将我的标记准确地添加到蓝点上。

这是我的 findMe 按钮代码:

ImageButton findMyLocation_btn = (ImageButton) findViewById(R.id.findme);
    if (findMyLocation_btn != null) {
        findMyLocation_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    buildAlertMessageNoGps();
                } else {
                    currentLocation = mMap.getMyLocation();

                    if ( marker1 == null && currentLocation != null) {
                        new onMyLocationClick().execute();
                    }
                }
            }
        });

和 onMyLocationClick()

private class onMyLocationClick extends AsyncTask{

    @Override
    protected Object doInBackground(Object[] params) {


            Geocoder gc = new Geocoder(MainActivity.this);
            List<Address> list = null;
            try {
                list = gc.getFromLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), 1);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            if (list != null) {
                add = list.get(0);
            }
            try {
                origin = new LatLng(add.getLatitude(),add.getLongitude());
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        return null;
    }

    @Override
    protected void onPostExecute (Object o){

            createMarker(add.getLatitude(), add.getLongitude(), add.getLocality(), add.getSubLocality());
        }
}

【问题讨论】:

    标签: android dictionary


    【解决方案1】:

    基于谷歌地图文档:

    此方法已被弃用。请改用 com.google.android.gms.location.FusedLocationProviderApi。 FusedLocationProviderApi 提供改进的位置查找和电源使用,并由“我的位置”蓝点使用。

    获取当前位置的代码是这样的:

    LocationManager locationManager = (LocationManager)
            getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    
    Location location = locationManager.getLastKnownLocation(locationManager
            .getBestProvider(criteria, false));
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    

    对于更高级的解决方案,您可以使用谷歌提到的 FusedLocationProvider:https://developer.android.com/training/location/retrieve-current.html#GetLocation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-06
      • 2014-12-08
      相关资源
      最近更新 更多