Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the position of a Marker on a Android Map v2

I need to do the following: I have a Marker on the map and I need to change the position of it. So I tried the following:

MarkerOptions a = new MarkerOptions()             .position(new LatLng(50,6)));         map.addMarker(a);         a.position(new LatLng(50,5)); 

where map is a GoogleMap. I think I have to refresh the map or somthing equal?

like image 382
gurehbgui Avatar asked Apr 09 '13 14:04

gurehbgui


People also ask

How do I drag a marker on Google Maps Android?

Long press the marker to enable dragging. When you take your finger off the screen, the marker will remain in that position. Markers are not draggable by default. You must explicitly set the marker to be draggable either with MarkerOptions.

How do you move a pointer on Google Maps?

Move around the map: Use the arrow keys. Tip: To move the map by one square, hold Shift and press the arrow keys.


2 Answers

Found the solution, Need to do it like this:

MarkerOptions a = new MarkerOptions()     .position(new LatLng(50,6))); Marker m = map.addMarker(a); m.setPosition(new LatLng(50,5)); 
like image 88
gurehbgui Avatar answered Sep 19 '22 08:09

gurehbgui


There's one example of moving marker in google map v2 demo app .. In file adt-bundle-linux/sdk/extras/google/google_play_services/samples/maps/src/com/exa‌​mple/mapdemo/MarkerDemoActivity.java (4.2.2 examples)

Here the code for moving a 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);                 }             }         }     }); } 

This code will animate the marker from one geopoint to another.

like image 21
K_Anas Avatar answered Sep 21 '22 08:09

K_Anas