Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

javascript - Google Map itinerary from geoJSON file

I'd like to draw an itinerary between 2 markers which are defined in this geoJSON file:

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

 [

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

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

}

The two markers are displayed well on the map.

Now I want to create an itinerary (car), between those two points.

I have this javascript function, which allows me to draw an itinerary from a form filled by the user:

function calculate() {

origin      =   document.getElementById('origin').value;
destination =   document.getElementById('destination').value;


var request = {
origin: origin,
destination: destination,


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

And now, I would like to replace the "origin" and "destination", by the 2 points defined in my geoJSON file, in order to create an itinerary between those two points.

Any idea ?

Thank you for your help !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One possible solution, use the Google Maps Data Layer to display your GeoJSON. The use it to retrieve the coordinates and get directions between them. The below code assumes 2 points (and uses your example with 2 points):

working fiddle

function calculate() {
    var request = {
        origin: origin,
        destination: destination,


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

// global variables
var origin = null;
var destination = null;
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 () {
        infowidow.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 if (!destination) {
                destination = e.feature.getGeometry().get();
                // calculate the directions once both origin and destination are set 
                calculate();
            }
        }
    });
    map.data.addGeoJson(data);
}

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...