2016-07-04 204 views
2

我在地图上有一个标记,我需要更改标记的位置。如何更改位置上的标记位置变化android

这里是我的代码:

public void onLocationChanged(Location location) { 

    map.clear(); 
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
    if (Home_Activity.this.markerob != null) { 
     Home_Activity.this.markerob.remove(); 
     markerob.setPosition(latLng); 
    } 
    latitude=location.getLatitude(); 
    longitude=location.getLongitude(); 
    CameraUpdate cameraUpdate =CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(18.0f).build()); 

    MarkerOptions options = new MarkerOptions().position(latLng); 
    //options.title(getAddressFromLatLng(latLng)); 

    options.icon(BitmapDescriptorFactory.fromBitmap(Bitmap.createScaledBitmap(
      BitmapFactory.decodeResource(getResources(), 
        spmain.getavatarimage()), 90, 90, false))); 

    //map.addMarker(new MarkerOptions().position(latLng)); 
    Toast.makeText(getApplicationContext(), "lat="+latLng,Toast.LENGTH_SHORT).show(); 

    options.position(latLng); 
    map.getUiSettings().setRotateGesturesEnabled(true); 
    map.animateCamera(cameraUpdate); 
    map.moveCamera(cameraUpdate); 
    map.addMarker(options); 


    //locationManager.removeUpdates(this); 
} 

我已经加入onLocationChanged方法,并在,我越来越位置的详细信息,但不能移动标记上的新位置。

+0

你是否在位置变化时获得更新的经纬度? –

回答

3

检查您的地图是否添加了标记(第一次)。如果不是,则添加标记并保留对该标记的引用。对于后续的onLocationChanged调用,只需使用相同的参考来更新纬度和经度。

Marker myMarker; 
    if(myMarker == null){ 
     marker = map.addMarker(options); 
    } 
    else { 
     marker.setPosition(new LatLng(location.getLatitude(),location.getLongitude())); 
    } 

希望这会有所帮助。让我知道这是行不通的。将发布更多相关的代码。