javascript - 来自 geoJSON 的 Google map 航点

标签 javascript json google-maps-api-3 geojson

我想从 geoJSON 文件加载行程。 目前,它可以工作,但只有两点。

但我需要添加 4 或 5 个航路点。我的代码只读取前两个点并将它们设置为起点和目的地。

这是我的代码

 google.maps.event.addListener(map.data, 'addfeature', function (e) {

    if (e.feature.getGeometry().getType() === 'Point') {
        map.setCenter(e.feature.getGeometry().get());

        if (!origin) origin = e.feature.getGeometry().get(); //if origin does not exist

        else if (!destination) {
            destination = e.feature.getGeometry().get();

            calculate();
        }
    }
});

有什么想法吗? 我必须创建一个循环吗? 或者更改航点的 json 代码?

这是我的 json :

{
"type": "FeatureCollection",
"features":

 [

    { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [-73.562686, 45.4960413]},
        "properties": {"prop0": "value0"}
    },


    { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [-73.568367, 45.4933086]},
        "properties": {"prop0": "value0"}
    }
]

  }

谢谢!

最佳答案

working fiddle

function calculate() {
    var request = {
        origin: origin,
        waypoints: waypts,
        destination: destination,
        travelMode: google.maps.TravelMode.DRIVING
    };
    directionsDisplay.setPanel(document.getElementById('directions-panel'));
    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        }
    });
}

// global variables
var origin = null;
var destination = null;
var waypts = [];
var infowindow = new google.maps.InfoWindow();
var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();

function initialize() {
    // Create a simple map.
    features = [];
    map = new google.maps.Map(document.getElementById('map-canvas'), {
        zoom: 4,
        center: {
            lat: -28,
            lng: 137.883
        }
    });
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById('directions-panel'));
    google.maps.event.addListener(map, 'click', function () {
        infowindow.close();
    });
    // process the loaded GeoJSON data.
    google.maps.event.addListener(map.data, 'addfeature', function (e) {
        if (e.feature.getGeometry().getType() === 'Point') {
            map.setCenter(e.feature.getGeometry().get());
            // set the origin to the first point
            if (!origin) origin = e.feature.getGeometry().get();
            // set the destination to the second point
            else waypts.push({
                location: e.feature.getGeometry().get(),
                stopover: true
            });
            // calculate the directions once both origin and destination are set 
            // calculate();
        }
    });
    google.maps.event.addListenerOnce(map, 'idle', function () {
        if (!destination) {
            destination = waypts.pop();
            destination = destination.location;
            // calculate the directions once both origin and destination are set 
            calculate();
        }
    });
    map.data.addGeoJson(data);
}

google.maps.event.addDomListener(window, 'load', initialize);

要解决 Dr.Molle 关于在加载数据层之前触发 idle 事件的观点,您可以创建一个自定义 data_idle 事件,并在所有数据层加载之后触发该事件。来自 GeoJson 的点已被处理。

updated fiddle

var features_added = 0;
function initialize() {
    // Create a simple map.
    features = [];
    map = new google.maps.Map(document.getElementById('map-canvas'), {
        zoom: 4,
        center: {
            lat: -28,
            lng: 137.883
        }
    });
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById('directions-panel'));
    google.maps.event.addListener(map, 'click', function () {
        infowindow.close();
    });
    // process the loaded GeoJSON data.
    google.maps.event.addListener(map.data, 'addfeature', function (e) {
        if (e.feature.getGeometry().getType() === 'Point') {
            features_added++;
            map.setCenter(e.feature.getGeometry().get());
            // set the origin to the first point
            if (!origin) origin = e.feature.getGeometry().get();
            // set the destination to the second point
            else waypts.push({
                location: e.feature.getGeometry().get(),
                stopover: true
            });
            setTimeout(function() {features_added--; if (features_added <= 0) google.maps.event.trigger(map, 'data_idle');
                }, 500);
        }
    });
    google.maps.event.addListenerOnce(map, 'data_idle', function () {
        if (!destination) {
            destination = waypts.pop();
            destination = destination.location;
            // calculate the directions once both origin and destination are set 
            calculate();
        }
    });
    map.data.loadGeoJson("http://www.geocodezip.com/directions.json.txt");
}

google.maps.event.addDomListener(window, 'load', initialize);

关于javascript - 来自 geoJSON 的 Google map 航点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25272247/

相关文章:

Javascript 函数比普通操作更快

c# - ServiceStack JsonSerializer 继承时不序列化对象成员

json - 关于 ember cli 如何获取 JSON 响应的体面教程

json - 在 R 中将大型数据框写为 json 的最快方法是什么?

google-maps - Google map 中多边形的工具提示

javascript - Google Maps JS API v3 和循环动画 gif

javascript - 调整窗口大小时切换图标的可见性

javascript - 建议一种快速解决方法,以防止 md-menu 在单击 md-menu-item 时关闭

javascript - 未捕获的 ReferenceError : google is not defined at google. maps.Marker()

java.lang.NullPointerException GoogleMaps V2