Reputation: 33
I am creating a Android app which will load the google map to webview. Than the user can create their own route by clicking (set waypoints) on map. Google supports up to 8 waypoint.
My question is, how can I handle the "click event" from map (set waypoint) and get the [lat, lon] of this waypoint? The goal is to store the clicked waypoints in array.
So far I did not find any tutorial which shows how can be added multiple waypoints into google maps from webview.
Upvotes: 0
Views: 600
Reputation: 17613
What you can do is enable the click listener using OnMapClickListener. When a user clicks (taps) somewhere on the map, you will receive an onMapClick(LatLng) event that indicates the location on the map that the user clicked
It's now up to you to store the LatLng values that the event has returned. Here's a snippet from this Google sample:
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
mMap.setOnMapClickListener(this);
mMap.setOnMapLongClickListener(this);
mMap.setOnCameraIdleListener(this);
}
//'point' holds the value of LatLng coordinates
@Override
public void onMapClick(LatLng point) {
mTapTextView.setText("tapped, point=" + point);
}
Here's the version in Javascript
function initMap() {
var myLatlng = {lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: myLatlng
});
map.addListener('click', function(e) {
console.log("clicked at " + e.latLng);
});
}
In this simple fiddle demo, if you click on the map and check your console log, you will see the exact coordinates where the event happened. So going back to your situation, if you click on a waypoint, you will know where it is located.
Upvotes: 1