error in displaying Directions webservice result on to the google map

前端 未结 1 935
猫巷女王i
猫巷女王i 2020-12-07 03:42

I am using webservices Direction Service API to display the route on google map. And not able to succefully execute the examples provided in the webservices Directions API d

相关标签:
1条回答
  • 2020-12-07 04:20

    The Google Maps Javascript API DirectionsRenderer doesn't render the response from Directions API web service

    From the documentation:

    DirectionsRenderer class

    Renders directions obtained from the DirectionsService.

    Use the Google Maps Javascript API v3 DirectionsService to get the route to render.

    proof of concept fiddle (with your route)

    code snippet:

    // var directionsUrl = "https://maps.googleapis.com/maps/api/directions/json?origin=sydney,au&destination=perth,au&waypoints=via:-37.81223%2C144.96254%7C-34.92788%2C138.60008
    
    var geocoder;
    var myMap;
    var stepDisplay;
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    
    function initialize() {
      setBaseMap();
      var request = {
        origin: "sydney,au",
        destination: "perth,au",
        waypoints: [{
          location: new google.maps.LatLng(-37.81223, 144.96254),
          stopover: false
        }, {
          location: new google.maps.LatLng(-34.92788, 138.60008),
          stopover: false
        }],
        travelMode: google.maps.TravelMode.DRIVING
      };
      directionsService.route(request, function(result, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          custimiseDriection(result, {});
        }
      });
    }
    google.maps.event.addDomListener(window, "load", initialize);
    
    function custimiseDriection(result, paramList) {
      //console.log(result);
      // var directionResult = JSON.parse(result);
      rendererOptions = {
        map: myMap,
        suppressMarkers: true,
        polylineOptions: {
          strokeColor: "red"
        }
      };
      var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
      directionsDisplay.setDirections(result);
    }
    
    function setBaseMap() {
      var mapOptions = {
        zoom: 10,
        center: new google.maps.LatLng(-33.8636979, 151.207455),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      myMap = new google.maps.Map(document.getElementById('map'), mapOptions);
      stepDisplay = new google.maps.InfoWindow();
    }
    html,
    body,
    #map {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
    <div id="map"></div>

    UPDATE:
    If you want to render the result using the DirectionsRenderer, there is a related question: Google Maps display route from json, that doesn't quite work out of the box (I get a javascript error: Uncaught TypeError: Cannot read property 'travelMode' of undefined as the request is now expected to be available, but it can be updated to work: proof of concept fiddle (with your route displayed via the DirectionsRenderer)). Note that this is not documented, so is probably not safe for production code as it may break at any time.

    0 讨论(0)
提交回复
热议问题