Move Markers in Google Map V2 Android

Move markers in google map v2 android

There's one example of moving marker in google map v2 demo app .. in the sample of the play library!!

I have looked into that!! here the code for moving an marker : -- >

    public void animateMarker(final Marker marker, final LatLng toPosition,
final boolean hideMarker) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = mGoogleMapObject.getProjection();
Point startPoint = proj.toScreenLocation(marker.getPosition());
final LatLng startLatLng = proj.fromScreenLocation(startPoint);
final long duration = 500;

final Interpolator interpolator = new LinearInterpolator();

handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
double lng = t * toPosition.longitude + (1 - t)
* startLatLng.longitude;
double lat = t * toPosition.latitude + (1 - t)
* startLatLng.latitude;
marker.setPosition(new LatLng(lat, lng));

if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarker) {
marker.setVisible(false);
} else {
marker.setVisible(true);
}
}
}
});
}

Hope it help every one!!

move marker on google maps api 2

Make variable

 Marker now;

in this part add Marker and remove marker, of course put in the rest of your marker attributes:

 @Override
public void onLocationChanged(Location location) {

if(now != null){
now.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);
now = 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));

}

Android Google Map v2 - move marker to clicked position and update geo coordinates

try this

 Marker marker;
GoogleMap mMap;

mMap.setOnMapClickListener(new OnMapClickListener() {

@Override
public void onMapClick(LatLng latlng) {
// TODO Auto-generated method stub

if (marker != null) {
marker.remove();
}
marker = mMap.addMarker(new MarkerOptions()
.position(latlng)
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
System.out.println(latlng);

}
});

how to move marker on google map in android based on locations comes from api

you can use it as,remove code from location change listener and put it somewhere like button just for checking purpose, later you can put it in your background service.

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
markerPoints.clear();

markerPoints.add(new LatLng(23.049543, 72.517195));
markerPoints.add(new LatLng(23.048457, 72.516787));
markerPoints.add(new LatLng(23.048989, 72.516973));
markerPoints.add(new LatLng(23.048263, 72.516667));
markerPoints.add(new LatLng(23.047409, 72.516281));
markerPoints.add(new LatLng(23.046219, 72.515696));

Bitmap Icon = BitmapFactory.decodeResource(getResources(), R.mipmap.bus);
setAnimation(mGoogleMap,markerPoints,Icon);
}
});

public static void setAnimation(GoogleMap myMap, final List<LatLng> directionPoint, final Bitmap bitmap) {

myMap.clear();
Marker marker = myMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
.position(directionPoint.get(0))
.flat(true));

myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(0), 16));

animateMarker(myMap, marker, directionPoint, false);
}

private static void animateMarker(GoogleMap myMap, final Marker marker, final List<LatLng> directionPoint,
final boolean hideMarker) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = myMap.getProjection();
final long duration = 30000;

final Interpolator interpolator = new LinearInterpolator();

handler.post(new Runnable() {
int i = 0;

@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
if (i < directionPoint.size())
marker.setPosition(directionPoint.get(i));
i++;

if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 150);
} else {
if (hideMarker) {
marker.setVisible(false);
} else {
marker.setVisible(true);
}
}
}
});
}

Move marker with gps in google map android

You can use below code to update position of the Marker

public void onLocationChanged(Location location) {

double lattitude = location.getLatitude();
double longitude = location.getLongitude();

//Place current location marker
LatLng latLng = new LatLng(lattitude, longitude);

if(mCurrLocationMarker!=null){
mCurrLocationMarker.setPosition(latLng);
}else{
mCurrLocationMarker = mGoogleMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
.title("I am here");
}

tv_loc.append("Lattitude: " + lattitude + " Longitude: " + longitude);
gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));

//stop location updates
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}

}

You don't need to clear map every time. You can do it by Marker object that is returned when adding Marker to Map.
Hope it will help you.

How to see other markers in google map moving android studio google maps

You may try using addChildEventListner instead

Map<String, Marker> markers = new HashMap();

ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
UsersActive uA = dataSnapshot.getValue(UsersActive.class);

// ...

Marker uAmarker = mMap.addMarker(markerOptions);
markers.put(dataSnapshot.getKey(), uAmarker);
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
UsersActive uA = dataSnapshot.getValue(UsersActive.class);

// ...

if (markers.contains(dataSnapshot.getKey())) {
Marker marker = markers.get(dataSnapshot.getKey());

marker.remove();
// or
// marker.setPosition(newPosition);
}

Marker uAmarker = mMap.addMarker(markerOptions);
markers.put(dataSnapshot.getKey(), uAmarker);
}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
if (markers.contains(dataSnapshot.getKey())) {
Marker marker = markers.get(dataSnapshot.getKey());
marker.remove();
}
}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

source

or try to refresh createMarker() in your onResume().



Related Topics



Leave a reply



Submit