简体   繁体   English

Google Maps-添加两个标记(方向)

[英]Google Maps - Add two markers (directions)

Playing around with Google Maps these days, with some directions. 这些天来玩Google地图,并提供一些指导。

I have a map that gets the directions and address (reverse-geocoding) when dragging and dropping the markers. 我有一张地图,可在拖放标记时获取路线和地址(反向地理编码)。

If there is two nodes on the map ( http://dev.korebogen.dk/gmap/ ) the script is working fine (click set directions) - but I need to add a click event so I can place those two markers instead of hard-code the location by hand, but still be able to drag them around - or place new with new click. 如果地图上有两个节点( http://dev.korebogen.dk/gmap/ ),脚本运行正常(单击设置方向)-但我需要添加一个click事件,以便可以放置这两个标记而不是手动对位置进行硬编码,但仍然可以拖动它们-或单击新按钮放置新位置。 But I only need A to B markers. 但是我只需要A到B标记。

Ive been playing around with some click events, but I can not seem to accomplish what I am looking for - hope to see some help here. 我一直在处理一些单击事件,但是我似乎无法完成我要找的事情-希望在这里看到一些帮助。 Thank you very much. 非常感谢你。

This code will allow you to click and place two markers, which you can then use to load GDirections, and remove the original markers. 此代码将允许您单击并放置两个标记,然后可以使用它们来加载GDirections,并删除原始标记。 Note that you must use this format for the query string: "from: marker@35,-25 to: marker@-20,15". 请注意,您必须对查询字符串使用以下格式:“从:marker @ 35,-25到:marker @ -20,15”。

var markerArray = [];

var listener = GEvent.addListener(map, "click", function(overlay, latlng) {
    var marker = new GMarker(latlng, { draggable: true });
    map.addOverlay(marker);
    markerArray.push(marker);
    if (markerArray.length > 1) {
        GEvent.removeListener(listener);
        var marker1 = markerArray[0];
        var marker2 = markerArray[1];

        gdir.load("from: marker1@" + marker1.getLatLng() + " to: marker2@" + marker2.getLatLng());

        map.removeOverlay(marker1);
        map.removeOverlay(marker2);
    }
});

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

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