简体   繁体   English

如何使超链接使用位置标记和信息更新嵌入式Google地图

[英]How to have a hyperlink update an embedded google map with a location marker and information

I've seen similar questions on here unanswered or they require a page refresh, I'm hoping I can get an answer from someone more familiar with the Google Maps API than I am, and I'd like this to work without having to refresh the page. 我在这里看到了类似的问题无法解答,或者需要刷新页面,希望我可以从比我更熟悉Google Maps API的人那里得到答案,并且我希望这种方法无需刷新就可以解决这一页。

I have an embedded Google map on a webpage and I have a list of locations in the HTML of the webpage. 我在网页上有一个嵌入式Google地图,并且在网页的HTML中有一个位置列表。

I would like it so that when a visitor clicks one of the locations it shows that location on the embedded map. 我希望这样,当访问者单击某个位置时,它会在嵌入式地图上显示该位置。

Sounds simple enough right? 听起来很简单吧? I have been reading documentation for 2 days on the API, and I can't seem to find what I am looking for. 我已经阅读了两天的API文档,但似乎找不到我想要的东西。

I'm anticipating being able to make a call to a JS function onClick of the link that will set a location/marker/InfoBox. 我希望能够调用将设置位置/标记/ InfoBox的链接的JS函数onClick。 All I basically need to know is how to communicate with the embbeded map via a link. 我基本上只需要知道如何通过链接与嵌入的地图进行通信。 I can sort out the infoBox and the rest myself. 我可以自己整理信息框,其余的可以整理。

Thanks a ton for reading! 非常感谢您的阅读!

In case you were wondering, I am using the following JS to embed the google map: 如果您想知道,我正在使用以下JS嵌入Google地图:

function initialize() {

    var mapOptions = {
        zoom: 8,
        panControl: false,
        rotateControl: false,
        scaleControl: false,
        streetViewControl: false,
        center: new google.maps.LatLng(51.004106,-114.135442),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }   
    var map = new google.maps.Map(document.getElementById("header"), mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);

You'll probably want to create a function to add the marker like this: 您可能需要创建一个函数来添加标记,如下所示:

function addMarker(map, lat, long, title, popupHtml) {
    var latLong = new google.maps.LatLng(lat, long);
    var markerOpts = { position: latLong, map: map, title: title };
    var marker = new google.maps.Marker(markerOpts);
    var infoWindow = new google.maps.InfoWindow();
    google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(popupHtml);
        infoWindow.open(map, marker);
    });
}

Then in the initialize function do something to attach the function to your links. 然后在initialize函数中执行一些操作以将该函数附加到链接。

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

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