Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

java - 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.

question from:https://stackoverflow.com/questions/65600867/opening-browser-url-when-clicking-a-marker-in-my-google-maps-activity-from-my-ow

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...