简体   繁体   English

Google地图:可点击的自定义标记

[英]Google Maps: Clickable Custom Marker

Upon creating many custom markers from within a loop, I cannot seem to find a solution to being able to link to external websites while each marker has a unique link. 从一个循环中创建许多自定义标记后,我似乎找不到能够在每个标记都有唯一链接的情况下链接到外部网站的解决方案。

I have tried many variations of this code, but seem to keep running into undefined variables (code below demonstrates undefined map_markers variable) 我已经尝试过此代码的许多变体,但似乎会不断遇到未定义的变量(下面的代码演示了未定义的map_markers变量)

  <script src="https://maps.googleapis.com/maps/api/js"></script> <script type="text/javascript"> var map; // generated by PHP, but output looks like this... var map_markers = [[43.536396, -110.740956, "T1", "/test-url"],[43.812092, -111.097181, "T2", "/test-url2"],[43.53640889360323, -110.74090051257326, "T3", "/test-url3"],[43.463373, -110.762519, "T4", "/test-url4"]]; google.maps.event.addDomListener(window, 'load', function() { var mapCanvas = document.getElementById('map'); map = new google.maps.Map(mapCanvas, { center: new google.maps.LatLng(37.8846194, -95.660041), zoom: 4, mapTypeId: google.maps.MapTypeId.ROADMAP }); for (var i = 0; i < map_markers.length; i++) { var marker = new google.maps.Marker({ position: {'lat': map_markers[i][0], 'lng': map_markers[i][1]}, map: map, title: map_markers[i][2] }); google.maps.event.addListener(marker, 'click', (function(markers, index) { return function() { window.open("http://www.example.com/" . markers[index][3], '_blank'); } })(map_markers, i)); } }); </script> 

Note: The code above uses an enclosure, but i've tried many variations like adding a "url" param to the marker (but this results in the url property being undefined?), i've tried this.url when having that property too, but no luck... 注意:上面的代码使用了一个附件,但是我尝试了许多变体,例如向标记添加“ url”参数(但这导致url属性未定义?),我在拥有该属性时尝试了this.url也是,但是没有运气...

Above code generates this error upon clicking a marker: Uncaught TypeError: Cannot read property '135' of undefined where 135 is the index number for the marker clicked 上面的代码在单击标记时会生成此错误: Uncaught TypeError:无法读取未定义的属性'135',其中135是单击的标记的索引号

I have searched the web, but no solution seems to fix my problem. 我已经在网上搜索过,但是似乎没有解决方案可以解决我的问题。 Maybe i'm missing something simple, but it's stumped me [?] 也许我缺少一些简单的东西,但是它让我很沮丧[?]

PS: The map and markers generate perfect, just the JS error upon clicking a marker that i'm having issues with! PS:地图和标记会产生完美的效果,只要单击我遇到问题的标记,它就会产生JS错误!

TIA! TIA!

Looks like 看起来像

window.open("http://www.example.com/" . markers[index][3], '_blank');

should be 应该

window.open(" http://www.example.com/ " + map_markers[index][3], '_blank'); window.open(“ http://www.example.com/ ” + map_markers [index] [3],'_blank');

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

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