第一次加载应用时,onMapReady纬度和经度为空

时间:2017-09-28 08:38:14

标签: android google-maps

我可以在移动标记上显示纬度和经度。当此onMapReady调用时,纬度和经度设置为zero

以下是代码:

public void onMapReady(GoogleMap googleMap) {
    Log.d(TAG, "OnMapReady");
    mMap = googleMap;

    mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {
            Log.d("Camera postion change" + "", cameraPosition + "");
            mCenterLatLong = cameraPosition.target;


            mMap.clear();

            try {

                Location mLocation = new Location("");
                mLocation.setLatitude(mCenterLatLong.latitude);
                mLocation.setLongitude(mCenterLatLong.longitude);

                startIntentService(mLocation);
                mLocationMarkerText.setText("Lat : " + mCenterLatLong.latitude + "," + "Long : " + mCenterLatLong.longitude);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }

}

我想在我打开应用程序时设置-34, 151 lat和long,否则我想在应用程序打开时获取当前位置。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

您可以在onMapReady(GoogleMap googleMap)

中添加此代码
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(-34, 151))
.zoom(13)   //optional if you want to zoom or not
.build();
MapFragment.newInstance(new GoogleMapOptions()
.camera(cameraPosition));

要获取地图上的当前位置,请检查此answer

答案 1 :(得分:1)

onMapReady(GoogleMap googleMap)

中添加以下代码行

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-34, 151), 12.0f));