简体   繁体   English

如何在多个标记中添加侦听器,这些标记调用相同的函数以在Google Maps API v3中单击时显示方向

[英]How do I add a listener to multiple markers which call the same function to display directions on click in google maps api v3

var x1 = 0;
var startPoint = new google.maps.LatLng(0, 0);
var endPoint = new google.maps.LatLng(0, 0);
var marker;
var latlngs = new Array();
var infowindow;
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
var locations = [
   {
       "name": "Frankie Johnnie & Luigo Too",
       "address": "939 W El Camino Real, Mountain View, CA",
       "lat": 37.386339,
       "lng": -122.085823
}, {
       "name": "Amici's East Coast Pizzeria",
       "address": "790 Castro St, Mountain View, CA",
       "lat": 37.38714,
       "lng": -122.083235
}, {
       "name": "Kapp's Pizza Bar & Grill",
       "address": "191 Castro St, Mountain View, CA",
       "lat": 37.393885,
       "lng": -122.078916
}, {
       "name": "Round Table Pizza: Mountain View",
       "address": "570 N Shoreline Blvd, Mountain View, CA",
       "lat": 37.402653,
       "lng": -122.079354
}];

Edit: these are the global variables just to clarify ^ 编辑:这些是全局变量,只是为了阐明^

I want create a "click on markers to get directions" functionality. 我要创建“单击标记以获取路线”功能。 So far I have created a list in JSON that creates all the markers from the "lat" and "long" in the list without a problem: 到目前为止,我已经在JSON中创建了一个列表,该列表可以毫无问题地从列表中的“ lat”和“ long”创建所有标记:

for (var k in locations) {
       latlngs[k] = new google.maps.LatLng(locations[k].lat, locations[k].lng);
       marker = new google.maps.Marker({
           position: latlngs[k],
           animation: google.maps.Animation.DROP,
           title: locations[k].name,
           map: map
       });
   };

The JSON list ( locations[k] ) is 132 locations in total. JSON列表(location [k])总共有132个位置。 I want to be able to click on a marker, save it as a start point for directions then wait for the secnond marker to be clicked on, which will be saved as an end point. 我希望能够单击一个标记,将其保存为路线的起点,然后等待单击secnond标记,该标记将另存为终点。 Clicking the second marker will calculate and show directions as Iv'e tried below: 单击第二个标记将按照下面的方法尝试计算并显示路线:

google.maps.event.addListener(marker, 'click', function () {
       if (x1 === 0) {
           startPoint = this.marker.position;
           var infowindow = new google.maps.InfoWindow({
               content: "Start",
               position: startPoint
           });
           infowindow.open(map, this.marker);
           directionsDisplay.setMap(map);
           directionsDisplay.setPanel(document.getElementById('panel'));
           x1++;
       } else {
           endPoint = this.marker.position;
           x1 = 0;
           calculateDirections(startPoint, endPoint);
       }
   });

At this point no infowindow gets displayed and I get the error saying "cannot read 'position' of undefined". 此时,没有显示任何信息窗口,并且出现错误消息“无法读取未定义的'位置'”。 I can get the directions to show when I hardcode the start and end points. 当我对起点和终点进行硬编码时,我可以获得指示的信息。

The following threads touches on the same idea but don't answer the listener for all markers problem, which I think is the main issue. 以下线程涉及相同的想法,但并未解决所有标记问题的侦听器,我认为这是主要问题。

Google Maps API V3 - add event listener to all markers? Google Maps API V3-将事件侦听器添加到所有标记吗?

and

how do I add same event listener to many markers and then differentiate between the markers in the listeners in google maps api v3? 如何将同一事件侦听器添加到许多标记,然后在Google Maps API v3的侦听器中区分标记?

If you get the LatLang on click then saving it wont be a big deal. 如果您点击LatLang,则保存起来不会有什么大不了的。 I have tried to achieve this on click and get the latlang. 我尝试在点击时实现这一目标并获得latlang。 I am not sure how much this will solve your problem but definitely will give you some idea to move ahead. 我不确定这能解决您的问题多少,但肯定会给您一些前进的想法。

Here is the working example, and I hope this will help you. 这是有效的示例,希望对您有所帮助。

  var map; var geocoder; var mapOptions = { center: new google.maps.LatLng(37.09024, -95.712891), zoom: 10, mapTypeId: google.maps.MapTypeId.ROADMAP }; function initialize() { var myOptions = { center: new google.maps.LatLng(37.09024, -95.712891), zoom: 10, mapTypeId: google.maps.MapTypeId.ROADMAP }; geocoder = new google.maps.Geocoder(); var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); google.maps.event.addListener(map, 'click', function(event) { placeMarker(event.latLng); }); var marker; function placeMarker(location) { if(marker){ //on vérifie si le marqueur existe marker.setPosition(location); //on change sa position }else{ marker = new google.maps.Marker({ //on créé le marqueur position: location, map: map }); } document.getElementById('lat').value=location.lat(); document.getElementById('lng').value=location.lng(); getAddress(location); } function getAddress(latLng) { geocoder.geocode( {'latLng': latLng}, function(results, status) { if(status == google.maps.GeocoderStatus.OK) { if(results[0]) { document.getElementById("address").value = results[0].formatted_address; } else { document.getElementById("address").value = "No results"; } } else { document.getElementById("address").value = status; } }); } } google.maps.event.addDomListener(window, 'load', initialize); 
 html, body, #map_canvas { margin: 0; padding: 0; height: 100% } 
 <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script><input type="text" id="address" size="30"><br><input type="text" id="lat" size="10"><input type="text" id="lng" size="10"> <div id="map_canvas"></div> 

暂无
暂无

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

相关问题 如何向多个标记添加相同的事件侦听器,然后区分 Google Maps API v3 中侦听器中的标记? - How do I add same event listener to many markers and then differentiate between the markers in the listeners in Google Maps API v3? 您可以使用Google Maps JavaScript API v3在同一页面上显示多个地图和路线吗? - Can you display multiple maps on the same page with directions using Google Maps JavaScript API v3? 使用Google Maps API v3显示多个标记 - Display multiple markers with Google maps API v3 谷歌地图 API V3 - 完全相同的位置上的多个标记 - Google maps API V3 - multiple markers on exact same spot 更改谷歌地图方向api V3中的单个标记 - Change individual markers in google maps directions api V3 使用Google Maps API v3添加多个标记 - Add multiple markers with Google Maps API v3 如何在Google Maps v3 API上异步添加标记? - How to add Markers on Google maps v3 API asynchronously? 如何更改Google Maps v3 API for Directions中的开始和结束标记图像 - How do I change the Start and End marker image in Google Maps v3 API for Directions 使用Google Maps JavaScript API V3在同一网页上显示两个带有方向的地图 - Display two maps with directions on the same web page using Google Maps JavaScript API V3 如何在Google Maps API v3中使用infobubbles()和选项卡显示多个标记? - how can i show multiple markers with infobubbles() & tabs in google maps api v3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM