简体   繁体   English

切换Google Map API 3中的位置

[英]switch locations in google map api 3

I have a Map with custom style and its working great, I was wondering if I can somehow control the location of the map using some links in my page, suppose I have 2 locations, I want to have 我有一个具有自定义样式的地图,它的工作效果很好,我想知道是否可以使用页面中的某些链接以某种方式控制地图的位置,假设我有两个位置,

<a href="#" id="locaation1">Location1</a>

&

<a href="#" id="locaation2">Location2</a>

Then when I click on Location 1 map goes to that location , and same story for location 2, is it possible with jquery? 然后,当我单击“位置1”时,地图将转到该位置,并且位置2的故事相同,是否可以使用jquery? It would also be great if I can add a custom marker to each location too. 如果我也可以在每个位置添加自定义标记,那就太好了。 Thanks. 谢谢。

Yes, you can even do it without jquery, the panto method is what you want to use, https://developers.google.com/maps/documentation/javascript/reference#Map basically, just add a call to a predefined function into your anchor onclick event (or use a click handler in jquery to wire it up). 是的,您甚至可以在没有jquery的情况下执行此操作,panto方法是您要使用的方法,基本上是https://developers.google.com/maps/documentation/javascript/reference#Map ,只需将对预定义函数的调用添加到您的锚点onclick事件(或在jquery中使用单击处理程序将其连接起来)。

using jquery, you would need something like: 使用jquery,您将需要以下内容:

$("#location1").click(function(){
   var pos = new google.maps.LatLng(-25.363882, 131.044922),
   g_map.panTo(pos);  //reference to globally defined google maps object
});

Here is a simple example from the google api documentation that can easily be retrofitted using the hints I have provided above: 这是google api文档中的一个简单示例,可以使用我上面提供的提示轻松进行改装:

https://google-developers.appspot.com/maps/documentation/javascript/examples/event-simple https://google-developers.appspot.com/maps/documentation/javascript/examples/event-simple

如果您的地图实例名为“地图”,则可以使用:

<a href="#" onclick="map.setCenter(new google.maps.LatLng(lat, long))" id="location1">Location1</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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