Updated 10 June 2017
In the last part of the working with AMaps, we learned about changing the map type and adding markers. Now we will learn about adding click listeners on the markers and showing InfoDialog on them.
After you have created the AMap and added the marker on it, You will have to implement an Interface called OnMarkerClickListener in your activity and override its onMarkerClick(Marker marker) function which will be called whenever a marker is clicked.
You can perform any kind of task like showing toasts or animating the marker. Let us see an example of this. After the implementation of the Interface, you need to set the click listener on your_amap.
1 |
your_amap.setOnMarkerClickListener(this); |
And now whenever your marker is clicked onMarkerClick(Marker marker) function will be called and you can perform the task in it. Look at the example given below
1 2 3 4 5 6 7 8 |
@Override public boolean onMarkerClick(final Marker marker) { if (aMap != null) { jumpPoint(marker); } Toast.makeText(your_activity.this, "Marker Clicked", Toast.LENGTH_LONG).show(); return true; } |
In the above code snippet, You can see that we have performed two actions on marker click. One is making a toast and the other one is making an animation of marker jumping. You can check the jumpPoint fucntion below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public void jumpPoint(final Marker marker) { final Handler handler = new Handler(); final long start = SystemClock.uptimeMillis(); Projection proj = aMap.getProjection(); final LatLng markerLatlng = marker.getPosition(); Point markerPoint = proj.toScreenLocation(markerLatlng); markerPoint.offset(0, -100); final LatLng startLatLng = proj.fromScreenLocation(markerPoint); final long duration = 1500; final Interpolator interpolator = new BounceInterpolator(); handler.post(new Runnable() { @Override public void run() { long elapsed = SystemClock.uptimeMillis() - start; float t = interpolator.getInterpolation((float) elapsed / duration); double lng = t * markerLatlng.longitude + (1 - t) * startLatLng.longitude; double lat = t * markerLatlng.latitude + (1 - t) * startLatLng.latitude; marker.setPosition(new LatLng(lat, lng)); if (t < 1.0) { handler.postDelayed(this, 16); } } }); } |
Now if you want to add an InfoWindow on the marker like showing some important information over the marker. It is quite simple. All you need to do is while creating your marker use title function for adding the title and snippet function for adding some details. Look at the below provided code snippet for example.
1 2 3 4 5 6 7 8 |
MarkerOptions markerOption = new MarkerOptions().icon(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_AZURE)) .position(latlng) <strong>.title("your_title") .snippet("your_text")</strong> .draggable(true); Marker marker = your_amap.addMarker(markerOption); marker.showInfoWindow(); |
That’s all for this section, We will study about more amazing AMap functionalities in the next sections
Thank you very much, this is Vedesh Kumar signing off.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.