When you add a marker to the map using addmarker
the method returns a Marker
object. So assume your code looks like:
Marker myMarker = mMap.addMarker(new MarkerOptions().position(louvre).title("French Musée du Louvre"));
Now with that myMarker
object you can now set a single piece of data (which it calls a tag
) as an Object
instance (which mean any kind of data) which will always be associated with that one specific marker. So one approach for you would be to set the tag
to the URL of the web page associated with the marker.
I assume you have the URLs for your markers at your disposable and so the code would then continue with the Louvre web as an example:
// Note the URL constructor can throw an MalformedException so you should handle that.
myMarker.setTag(new URL("https://www.louvre.fr/en/"));
Repeat this for each marker you add.
OK - that covers what is needed when adding markers.
Now to handling marker clicks. There is a single marker-click handler for the entire map instance and it is set as follows (likely in your onMapReady
). The someMarker
parameter is used to differentiate which marker. :
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
{
@Override
public boolean onMarkerClick(Marker someMarker) {
// Some defensive checks
if (someMarker != null && someMarker.getTag() != null && someMarker.getTag() instanceof URL) {
URL museumURL = (URL)someMarker.getTag();
// on to next part - what to do with the URL
}
return true;
}
});
Now once you have the URL (in the onMarkerClick
callback) as a result of the specific marker clicked you can now launch an intent to bring up a web browser for that URL:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(museumURL));
// 'mContext' is your activity instance - remember you are in a callback so 'this' is not correct but 'MyActivity.this' would be correct.
mContext.startActivity(intent);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…