【问题标题】:Custom Google Map markers on Directions APIDirections API 上的自定义 Google 地图标记
【发布时间】:2012-10-26 11:12:17
【问题描述】:

我对 Google 地图 api 完全陌生,在汇总了网上找到的几个示例后,我正在努力解决向方向添加自定义标记的最后一道障碍。这是我的代码:

var directionDisplay;
var directionsService = new google.maps.DirectionsService();
function initialize() {
    var latlng = new google.maps.LatLng([[showMapLatitude]],[[showMapLongitude]]);
    directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers: true});
    var myOptions = {
        zoom: 14,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        mapTypeControl: false
    };

var stylez = [
{
  featureType: "all",
  elementType: "all",
  stylers: [
    { saturation: -100 }
  ]
}
];


var image = new google.maps.MarkerImage('/icon.png',
    new google.maps.Size(20, 33),
    new google.maps.Point(0,0),
    new google.maps.Point(10,33)
);

    var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
            var mapType = new google.maps.StyledMapType(stylez, { name:"Grayscale" });
            map.mapTypes.set('tehgrayz', mapType);
            map.setMapTypeId('tehgrayz');    
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("directionsPanel"));
    var marker = new google.maps.Marker({
        position: latlng, 
        map: map, 
        title:"[[*pagetitle]]",
                    icon: image
    });

}



function calcRoute() {
            $(".storeDetails").hide();
            $(".storeAdress").hide();
            $(".backtocontact").show();
    var start = document.getElementById("routeStart").value;
    var end = "[[showMapLatitude]],[[showMapLongitude]]";
    var request = {
        origin:start,
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        }
    });
}

这里有一个例子:Change individual markers in google maps directions api V3

但是作为一个菜鸟,我似乎无法将它放在正确的位置,它要么出错,要么什么都不做。

【问题讨论】:

标签: google-maps google-maps-api-3 google-maps-markers


【解决方案1】:

改变

directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
    }
});

收件人

directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
        var leg = response.routes[ 0 ].legs[ 0 ];
        makeMarker( leg.start_location, icons.start, "title" );
        makeMarker( leg.end_location, icons.end, 'title' );
    }
});

别忘了添加makeMarker() 函数。 您还需要开始和结束图标

【讨论】:

  • 感谢大卫的帮助,但是客户总是改变主意,现在想要方向上的标准标记。然而,有一个新的错误,原来的自定义标记在方向加载后仍然可见。知道如何在方向加载时隐藏它吗?
【解决方案2】:

回复my other answer中的评论

移除原来的自定义标记并显示默认标记

删除

var image = new google.maps.MarkerImage('/icon.png',
new google.maps.Size(20, 33),
new google.maps.Point(0,0),
new google.maps.Point(10,33)
);

var marker = new google.maps.Marker({
    position: latlng, 
    map: map, 
    title:"[[*pagetitle]]",
                icon: image
});

也要改变

directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers: true});

收件人

directionsDisplay = new google.maps.DirectionsRenderer();

【讨论】:

  • 感谢大卫再次回复,非常感谢,但我的意思是在方向显示上,隐藏原始自定义标记(图像变量),留下标准的谷歌方向标记。我尝试在 calcRoute 函数中将 marker.map 设置为 null,但它找不到标记 var,即使我在初始化函数之外设置它(这将使它成为全局,除非我弄错了?)。
  • 您不需要 Google 提供的标记。您需要在回答中删除supressMarkers。
  • 对不起,我没有解释自己。到目前为止,我的地图显示了位置,带有一个自定义标记(客户想要的),以及一个输入框,当它被触发时,它会从输入的地址指示到该自定义标记。发生这种情况时,Google 会放弃标准方向标记,但自定义标记仍然可见,只是位置略有不同。客户希望仅在显示方向后丢失自定义标记。希望这是有道理的。顺便说一句,我已经删除了抑制标记。
  • 要删除标记,您需要将标记推入数组,请参阅http://stackoverflow.com/questions/1544739/google-maps-api-v3-how-to-remove-all-markersfunction calcRoute() { // First, remove any existing markers from the map. for (i = 0; i < markerArray.length; i++) { markerArray[i].setMap(null); } // Now, clear the array itself. markerArray = [];
猜你喜欢
  • 2019-03-09
  • 2016-02-05
  • 1970-01-01
  • 2019-06-05
  • 1970-01-01
  • 2017-04-10
  • 1970-01-01
  • 2016-06-10
  • 1970-01-01
相关资源
最近更新 更多