[英]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.我正在为一个大学项目做某种艺术收藏家应用程序,直到现在学习 Android 真的很有趣。 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.到目前为止,我创建了 Google Maps Activity 并设置了标记,但我不知道如何访问它们并在它们上放置一个 onClickListener,以及如何将其定向到网站。 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.所以我想更直截了当,我的问题是,我如何访问标记以及如何为每个标记设置侦听器以指向一些已知的 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. OnCreate 尚未修改。
When you add a marker to the map using addmarker
the method returns a Marker
object.当您使用 addmarker 将标记添加到addmarker
,该方法将返回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.现在有了myMarker
object,您现在可以将单个数据(它称为tag
)设置为Object
实例(表示任何类型的数据),该实例将始终与该特定标记相关联。 So one approach for you would be to set the tag
to the URL of the web page associated with the marker.因此,一种方法是将tag
设置为与标记关联的 web 页面的 URL 页面。
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:我假设您在一次性用品上有标记的 URL,因此代码将继续以卢浮宫 web 为例:
// 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
).整个 map 实例有一个标记单击处理程序,它的设置如下(可能在您的onMapReady
中)。 The someMarker
parameter is used to differentiate which marker. someMarker
参数用于区分哪个标记。 : :
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:现在,由于单击特定标记而获得 URL(在onMarkerClick
回调中),您现在可以启动一个意图,为该 ZE6B391A8D2C4D45902A23A8B6 调出 web 浏览器
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.