简体   繁体   中英

Opening browser url when clicking a marker in my Google Maps Activity from my own Android App

I'm doing some sort of Art Collectors App for a college project and it's really fun learning Android until now. Even though it's not on the TO-DO list from my teacher I think it would be really cool if I had a google maps Activity which shows where some of the top museums are and when you click the marker for that museum it opens the browser to the website of said museum. Until now I created the Google Maps Activity and I set up the markers but I can't figure out how to acces them and put an onClickListener on them, and also how to direct it to the website. If you could please point me in the right direction on this I would really appreaciate it. So I guess to be more straight to the point, my questions are, how do I acces the markers and how do I set up the listener for each one to point to some already known URL. My code for now:

    @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    LatLng louvre = new LatLng(48.8606, 2.3376);
    LatLng metropolitanMuseumOfArt = new LatLng(40.7794, -73.9632);
    LatLng theNationalGallery = new LatLng(51.5089, -0.1283);
    LatLng theNationalArtCenter = new LatLng(35.6653, 139.7264);
    mMap.addMarker(new MarkerOptions().position(louvre).title("French Musée du Louvre"));
    mMap.addMarker(new MarkerOptions().position(metropolitanMuseumOfArt).title("The Metropolitan Museum of Art"));
    mMap.addMarker(new MarkerOptions().position(theNationalGallery).title("The National Gallery"));
    mMap.addMarker(new MarkerOptions().position(theNationalArtCenter).title("The National Art Center"));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(louvre, 10f));
}

The OnCreate was not modified yet.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM