Google地图我正在获取当前位置,当我更改位置标记不会在谷歌地图中移动

时间:2017-03-04 08:11:34

标签: android

这是我的代码:

public class MapViewFragment extends Fragment implements OnMapReadyCallback {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.maps, container, false);

    Context context =this.getContext();
    GPSService mGPSService = new GPSService(context);
    mGPSService.getLocation();

    if (mGPSService.isLocationAvailable == false) {

    } else {

        // Getting location co-ordinates
         lat1 = mGPSService.getLatitude();
         lng1 = mGPSService.getLongitude();

    }

    mMapView.onCreate(savedInstanceState);
    ActivityCompat.requestPermissions(getActivity(),new String[]{
            android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    call.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
             call();
        }

    });
    mMapView.getMapAsync(this); //this is important
               }
    });*/

   }
    return v;
}     
  @Override

public void onMapReady(GoogleMap googleMap) {
    mGoogleMap = googleMap;
    mGoogleMap.getUiSettings().setZoomControlsEnabled(true);

     m1 = googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(lat1,lng1))
             .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
             .anchor(0.5f, 0.5f)
            .title("Title1")
            .snippet("Snippet1"));

@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
}



@Override
public void onDestroy() {
    super.onDestroy();
    mMapView.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}

   @Override
   public void onLowMemory() {
       super.onLowMemory();
       mMapView.onLowMemory();
   }
}

在程序中我获取当前位置lat和lang。当我移动到其他位置标记时不会改变。每当我移动位置都要改变。 每当我移动位置值时获得的当前位置值都不会改变。想要实现任何方法改变位置。

1 个答案:

答案 0 :(得分:0)

我假设您已经创建了一个标记来指示您当前的位置。像这样 标记currentLocation;

现在你需要实现LocationListener并在onLocationChanged回调中更新你的MapView

@Override
public void onLocationChanged(Location location) {
if(currentLocation != null){
            currentLocation.remove();
        }

TextView tvLocation = (TextView) findViewById(R.id.tv_location);

// Getting latitude of the current location
double latitude = location.getLatitude();

// Getting longitude of the current location
double longitude = location.getLongitude();

// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
currentLocation = googleMap.addMarker(new MarkerOptions().position(latLng)));
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}