在构建GoogleApiClient之前调用onMapReady()方法 - 获取LatLong 0.0

时间:2017-09-08 10:41:46

标签: android google-maps location android-mapview latitude-longitude

您好我在may app中获得了LatLong,如下所示:

在我的片段中创建了一个方法来构建GoogleApiClient:

 private synchronized void buildGoogleApiClient() {
    MyApp.getGoogleApiHelper().setConnectionListener(new GoogleApiHelper.ConnectionListener() {
        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

        }

        @Override
        public void onConnectionSuspended(int i) {

        }

        @Override
        public void onConnected(Bundle bundle) {
            //this function will call whenever google api connected or already connected when setting listener
            //You are connected do what ever you want
            //Like i get last known location
            try {
                if (!Constant.checkPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)) {
                    Constant.requestPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION, PLACE_PICKER_REQUEST);
                } else {
                    if (Constant.isOnline(mContext)) {
                        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
                        mCurrentLatitude = mLastLocation.getLatitude();
                        mCurrentLongitude = mLastLocation.getLongitude();
                        locationUpdate();
                    } else {
                        Constant.displayToast(mContext, getResources().getString(R.string.msg_internet));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

OnMapReady()方法如下:调用loadDefault()方法加载当前位置。

public void onMapReady(final GoogleMap googleMap) {
    loadDefault(googleMap);
}

我的loadDefault()方法如下:

private void loadDefault(GoogleMap googleMap) {
    try {
        if (Prefrences.checkPref(mContext, NEAR_ME_SEARCH))
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.parseDouble(Prefrences.getPref(mContext, NEAR_ME_LAT)), Double.parseDouble(Prefrences.getPref(mContext, NEAR_ME_LNG))), 8));
        else
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mCurrentLatitude, mCurrentLongitude), 8f));
    } catch (MalformedParameterizedTypeException e) {
        handleException(mContext, e);
    } catch (Exception e) {
        handleException(mContext, e);
    }
}

但是,我在loadDefault()方法的下面一行得到 mCurrentLaltitude mCurrentLongitude 0.0。

googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mCurrentLatitude, mCurrentLongitude), 8f));
请提供解决方案。感谢。

编辑

 private void initialization(View rootView, Bundle savedInstanceState) {
    try {
        mModelBarList = BarListModel.getBarListInstance();
        if (!Constant.checkPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)) {
            Constant.requestPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION, PLACE_PICKER_REQUEST);
        } else {
            if (Constant.isOnline(mContext)) {
                mGoogleApiClient = MyApp.getGoogleApiHelper().getGoogleApiClient();
                buildGoogleApiClient();
            } else {
                Constant.displayToast(mContext, getResources().getString(R.string.msg_internet));
            }
        }

        mMapView = (MapView) rootView.findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);
        mMapView.onResume();
        mMapView.getMapAsync(MapFragment.this);
}

1 个答案:

答案 0 :(得分:0)

调用onMapReady并加载地图时,不确定是否已连接google api客户端,因为这两个操作是异步执行的。所以你必须添加一个逻辑,这样那些完成的逻辑首先等到另一个结束,然后在地图中显示坐标。