Java – how to change the tag location when the location in Android changes

I have a mark on the map and need to change the position of the mark

This is my code:

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);
}

I added the onlocationchanged method to get the location details, but do not move the tag to the new location

resolvent:

Check that your map is not marked (for the first time). If not, add a mark and keep the reference to the mark. For subsequent onlocationchanged calls, just update the latitude and longitude with the same reference

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

Hope this helps. Let me know it doesn't work. More relevant code will be released

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>