繁体   English   中英

无法自动在Google地图上显示当前位置?

[英]Unable to show current location on google map automatically?

我试图在andorid上的谷歌地图上显示当前位置。我已经写了一些鳕鱼来做那件事。但是当我在地图活动上启动应用程序时,它没有在谷歌地图上显示当前位置。但是当我点击当前位置按钮时谷歌地图然后它显示当前位置。请问为什么它不会自动显示当前位置?

 mMap.setMyLocationEnabled(true);
                mMap.getUiSettings().setMyLocationButtonEnabled(true);
                Log.i("MAP", "AFTER LOCATION");
                LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
                Criteria criteria = new Criteria();
                criteria.setAccuracy(Criteria.ACCURACY_FINE);
                String bestProvider = locationManager.getBestProvider(criteria, true);
                Location location = locationManager.getLastKnownLocation(bestProvider);
                if (location != null) {

                    onLocationChanged(location);
                }
                locationManager.requestLocationUpdates(bestProvider,0, 0, this);

在此输入图像描述

地图准备好后使用此功能:

private void centerMap(GoogleMap map, Double latitude, Double longitude){

    LatLng latLng = new LatLng(latitude, longitude);

    CameraPosition cp = new CameraPosition.Builder()
            .target(latLng)
            .zoom(13)
            .build();

    map.animateCamera(CameraUpdateFactory.newCameraPosition(cp));
}

您可以从以下位置获取坐标:location.getLatitude()和Location.getLongitude()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM