How do I get JSON from google directions api using jQuery?

前端 未结 3 929
被撕碎了的回忆
被撕碎了的回忆 2020-12-12 08:07



  
  jQuery.getJSON demo
  

        
3条回答
  •  猫巷女王i
    2020-12-12 08:17

    You cannot use ajax to access googles maps api. It will give you an unknown error response but in reality its an "access denied" due to CORS. The below code will give you valid data for the route between brooklyn and queens, driving, in metrics

    
            var directionsService = new google.maps.DirectionsService();
            var directionsRequest = {
                origin: "brooklyn",
                destination: "queens",
                travelMode: google.maps.DirectionsTravelMode.DRIVING,
                unitSystem: google.maps.UnitSystem.METRIC
            };
            directionsService.route(directionsRequest, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {                    
                //do work with response data
                }
                else
                    //Error has occured
            })
    

    reference: http://www.sitepoint.com/find-a-route-using-the-geolocation-and-the-google-maps-api/

提交回复
热议问题