为路线上的Google地图标记(起点和终点)设置信息窗口

罪恶

嗨,我在我的代码中的两个标记之间绘制了一条路线。我需要做的是向这两个标记添加一个click事件,并为每个标记设置一个信息窗口。我在许多网站上搜索都找不到解决方案。尽管它会绘制标记之间的路线,但无法为每个标记设置信息窗口。这是我的代码...

                   function mapLocation() {
                        var directionsDisplay;
                        var directionsService = new google.maps.DirectionsService();
                        var map;

                        function initialize() {
                            directionsDisplay = new google.maps.DirectionsRenderer();
                            var chicago = new google.maps.LatLng(37.334818, -121.884886);
                            var mapOptions = {
                                zoom: 7,
                                center: chicago
                            };
                            map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
                            directionsDisplay.setMap(map);
                            google.maps.event.addDomListener(document.getElementById('routebtn'), 'click', calcRoute);
                        }

                        function calcRoute() {
                            var start = new google.maps.LatLng(37.334818, -121.884886);
                            //var end = new google.maps.LatLng(38.334818, -181.884886);
                            var end = new google.maps.LatLng(37.441883, -122.143019);
                            var request = {
                                origin: start,
                                destination: end,
                                travelMode: google.maps.TravelMode.DRIVING
                            };
                            directionsService.route(request, function (response, status) {
                                if (status == google.maps.DirectionsStatus.OK) {
                                    directionsDisplay.setDirections(response);
                                    directionsDisplay.setMap(map);
                                } else {
                                    alert("Directions Request from " + start.toUrlValue(6) + " to " + end.toUrlValue(6) + " failed: " + status);
                                }
                            });
                        }

                        google.maps.event.addDomListener(window, 'load', initialize);
                    }
                    mapLocation();
地理编码

您不能仅通过InfoWindow添加点击侦听器。您需要使用{suppressMarkers}DirectionsRenderer选项,然后解析响应中所需的信息以添加自己的标记。

根据我的示例进行了修改,网址为:http : //www.geocodezip.com/v3_directions_custom_iconsC.html

代码段:

function mapLocation() {
  var directionsDisplay;
  var directionsService = new google.maps.DirectionsService();
  var map;
  var infowindow;

  function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer({
      suppressMarkers: true
    });
    var chicago = new google.maps.LatLng(37.334818, -121.884886);
    var mapOptions = {
      zoom: 7,
      center: chicago
    };
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    infowindow = new google.maps.InfoWindow();
    directionsDisplay.setMap(map);
    google.maps.event.addDomListener(document.getElementById('routebtn'), 'click', calcRoute);
  }

  function calcRoute() {
    var start = new google.maps.LatLng(37.334818, -121.884886);
    var end = new google.maps.LatLng(37.441883, -122.143019);
    var waypoint = {
      location: new google.maps.LatLng(37.432334, -121.899574)
    };
    var waypoint2 = {
      location: new google.maps.LatLng(37.54827, -121.988572)
    };
    var request = {
      origin: start,
      destination: end,
      waypoints: [waypoint, waypoint2],
      travelMode: google.maps.TravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
        directionsDisplay.setMap(map);

        var startLocation = new Object();
        var endLocation = new Object();
        var waypointLocations = [];

        // Display start and end markers for the route.
        var legs = response.routes[0].legs;
        for (i = 0; i < legs.length; i++) {
          if (i == 0) {
            startLocation.latlng = legs[i].start_location;
            startLocation.address = legs[i].start_address;
            // createMarker(legs[i].start_location, "start", legs[i].start_address, "green");
          }
          if (i != 0 && i != legs.length - 1) { 
            var waypoint = {};
            waypoint.latlng = legs[i].start_location;
            waypoint.address = legs[i].start_address;
            waypointLocations.push(waypoint);
          }
          if (i == legs.length - 1) {
            endLocation.latlng = legs[i].end_location;
            endLocation.address = legs[i].end_address;
          }
          var steps = legs[i].steps;
        }
        createMarker(endLocation.latlng, "end", "special text for end marker", "http://www.google.com/mapfiles/markerB.png")
        createMarker(startLocation.latlng, "start", "special text for start marker", "http://maps.gstatic.com/mapfiles/markers2/marker_greenA.png");
        for (var i = 0; i < waypointLocations.length; i++) {
          createMarker(waypointLocations[i].latlng, "waypoint " + i, "special text for waypoint marker " + i, "http://www.google.com/mapfiles/marker_yellow.png");
        }
      } else {
        alert("Directions Request from " + start.toUrlValue(6) + " to " + end.toUrlValue(6) + " failed: " + status);
      }
    });
  }

  function createMarker(latlng, label, html, url) {
    var contentString = '<b>' + label + '</b><br>' + html;
    var marker = new google.maps.Marker({
      position: latlng,
      map: map,
      icon: url,
      title: label,
      zIndex: Math.round(latlng.lat() * -100000) << 5
    });

    google.maps.event.addListener(marker, 'click', function() {
      infowindow.setContent(contentString);
      infowindow.open(map, marker);
    });
  }
  google.maps.event.addDomListener(window, 'load', initialize);
}


// http://www.google.com/mapfiles/markerB.png


mapLocation();
html,
body,
#map-canvas {
  height: 100%;
  width: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<input id="routebtn" type="button" value="route" />
<div id="map-canvas"></div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何预填地图框路线API的起点和终点?

Google地图标记-“信息”窗口需要删除标记

为JSON地图中的数据设置Google Maps标记的自定义信息窗口

如何包括起点和终点的UI窗口-Google Maps应用

Android在折线的起点和终点放置标记

在ggplot中标记起点和终点

谷歌地图从输入文本字段标记航点起点和终点

无论我设置什么起点和终点,我总是得到一条路线

如何在Google地图中不显示标记图标的情况下显示信息窗口

Google地图为每个地图添加了多个标记和信息窗口?

外部点击如何关闭地图标记信息窗口

拖动标记时获得起点和终点的纬度和经度

使用多个标记和信息窗口自定义Google地图

单击目的地后创建从起点到目的地地图标记的路线线(驾驶)

如何实施 Google 地图标记详细信息?*图片*

单张中心弹出窗口和地图标记

如何在谷歌地图上添加信息窗口和标记

如何使用mapbox-gl-directions设置起点和终点?

按日期范围扩展行,以熊猫为起点和终点

如何在Google地图中的折线上方显示信息窗口?

如何动态设置Google地图标记大小

将动态信息窗口设置为动态标记的 onclick 函数

底部没有小三角形的地图标记信息窗口

无法设置Google地图的自定义信息窗口

在起点和终点之间过滤

如何在标记后面设置信息框 - React Google Maps

如何为所有移动设备创建一个链接,以打开以当前位置为起点并以给定位置为起点的路线的Google地图?

如何在路线上显示时间为谷歌地图

Leaflet Routing Machine API-以航点作为起点和终点的路线