从位置单独的地图不会更新onCreate上的位置

时间:2018-03-11 13:18:22

标签: android android-location android-maps-v2

这是我目前使用的getDeviceLocation

 private void getDeviceLocation() {
    /*
     * Get the best and most recent location of the device, which may be null in rare
     * cases when a location is not available.
     */
    try {
      if (mLocationPermissionGranted) {
        Task<Location> locationResult = mFusedLocationProviderClient.getLastLocation();
        locationResult.addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
          @Override
          public void onSuccess(Location location) {
            if (location != null) {
              SET_LOCATION_STATUS = 1;
              mMap.clear();
              // Set the map's camera position to the current location of the device.
              mLastKnownLocation = location;//task.getResult();
             // showMap() method starts
              mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                  new LatLng(mLastKnownLocation.getLatitude(),
                      mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
              marker = mMap.addMarker(new MarkerOptions()
                  .position(new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()))
                  .anchor(0.5f, 0.5f)
                  .icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon))
              );
              //showMap() method ends
              latlang.Lat = mLastKnownLocation.getLatitude();
              latlang.Lang = mLastKnownLocation.getLongitude();
            } else {
            ...}

我试图使用另一种方法将地点与地图部分分开:

/*
  Attempt to seperate location and map
  */
  private void showMap(){
    if(MAP_LOCATION_SOURCE == 1){
      getDeviceLocation();
    }
    if (SET_LOCATION_STATUS ==1 ) {
      mMap.clear();
      Log.v("Location Available??????????", Double.toString(latlang.Lat));
      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
          new LatLng(latlang.Lat, latlang.Lang), DEFAULT_ZOOM));
      marker = mMap.addMarker(new MarkerOptions()
          .position(new LatLng(latlang.Lat, latlang.Lang))
          .anchor(0.5f, 0.5f)
          .icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon))
      );
    }
  }

它们被称为/用作:

   @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setHasOptionsMenu(true);

      // Retrieve location and camera position from saved instance state.
      if (savedInstanceState != null) {
        mLastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION);
        mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
        SET_LOCATION_STATUS = 1;
      }
    }
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View view = inflater.inflate(R.layout.fragment_second, null, false);
    onSaveInstanceState(new Bundle());

    // Construct a FusedLocationProviderClient.
    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getContext());
    getLocationPermission();
    getDeviceLocation();
//    showMap();

  @Override
  public void onMapReady(GoogleMap map) {
    mMap = map;

    getLocationPermission();
    updateLocationUI();
    getDeviceLocation();
//    showMap();

这没有任何错误,但是没有像Bundle中的位置/摄像机位置一样,就像map在getDeviceLocation内部一样。当我使用应用程序开始使用时,位置和地图会更新。

如何正确分隔位置和地图呢?

0 个答案:

没有答案