地图问题:绘制2个坐标的路线

时间:2014-07-08 02:20:28

标签: javascript google-maps google-maps-api-3

很抱歉,如果之前已经提出过这个问题。我想加载一个带有2个标记的地图,起点和终点。建立2个标记时,我画了一条路径(模式:驱动器)。

当我选择2个标记时,我收到此错误:“Uncaught InvalidValueError:in property origin:not a string;而不是LatLng或LatLngLiteral:不是对象”请帮助我!

<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>

<script>
        var map;
        var cont=0;
        var directionsService = new google.maps.DirectionsService();
        function initialize() {
            var mapOptions = {
                zoom: 8,
                center: new google.maps.LatLng(-34.397, 150.644)
            };
        map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);

        google.maps.event.addListener(map, "click", daclick);


        function daclick(evento){
            //var cont=0;  
            if(cont==0){
                var latitudOrigen = evento.latLng.lat();
                var longitudOrigen = evento.latLng.lng();
                var myLatlngOrigen = new google.maps.LatLng(latitudOrigen,longitudOrigen);
                var markerOrigen = new google.maps.Marker({
                    position: myLatlngOrigen,
                    map: map,
                    title: "Origen"
  }); 
                cont=1;
            }else{
            var latitudDestino = evento.latLng.lat();
            var longitudDestino = evento.latLng.lng();
            var myLatlngDestino= new google.maps.LatLng(latitudDestino,longitudDestino);
            var markerDestino = new google.maps.Marker({
                    position: myLatlngDestino,
                    map: map,
                    title: "Destino"
  }); 
            cont=0;
      }

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

        }

    }
    google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

3 个答案:

答案 0 :(得分:2)

我在your codeUncaught InvalidValueError: in property origin: not a string; and not a LatLng or LatLngLiteral: not an Object时遇到此javascript错误。出于多种原因:

    var request = {
        origin: latitudOrigen,
        destination: myLatlngDestino,
        travelMode: google.maps.TravelMode.DRIVING
    };
  1. latitudOrigen不是google.maps.LatLng对象(它是单个数字,原点的纬度)。
  2. myLatlngOrigen在第二次点击时超出范围
  3. 最简单的解决方法是将您的标记设为全局,然后在运行路线服务时获取标记的位置:

            var request = {
                origin: markerOrigen.getPosition(),
                destination: markerDestino.getPosition(),
                travelMode: google.maps.TravelMode.DRIVING
            };
    
    • 您还需要在Click事件处理函数的else中移动DirectionsService的调用,因为在您同时拥有原点和目标之前它将无效。

      } else {
          var myLatlngDestino = evento.latLng;
          markerDestino = new google.maps.Marker({
              position: myLatlngDestino,
              map: map,
              title: "Destino"
          });
          var request = {
              origin: markerOrigen.getPosition(),
              destination: markerDestino.getPosition(),
              travelMode: google.maps.TravelMode.DRIVING
          };
          directionsService.route(request, function (response, status) {
              if (status == google.maps.DirectionsStatus.OK) {
                  directionsDisplay.setDirections(response);
              }
          });
          cont = 0;
      }
      

    然后我得到:Uncaught ReferenceError: directionsDisplay is not defined,因为你没有初始化directionsDisplay对象。

    一旦我确定它有效。

    working fiddle

    代码段

    var map;
    var cont = 0;
    var directionsService = new google.maps.DirectionsService();
    var markerOrigen = null;
    var markerDestino = null;
    var directionsDisplay = new google.maps.DirectionsRenderer();
    
    function initialize() {
      var mapOptions = {
        zoom: 8,
        center: new google.maps.LatLng(-34.397, 150.644)
      };
      map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
      directionsDisplay.setMap(map);
    
      google.maps.event.addListener(map, "click", daclick);
    
    
      function daclick(evento) {
        //var cont=0;  
        if (cont == 0) {
          var myLatlngOrigen = evento.latLng;
          markerOrigen = new google.maps.Marker({
            position: myLatlngOrigen,
            map: map,
            title: "Origen"
          });
          cont = 1;
        } else {
          var myLatlngDestino = evento.latLng;
          markerDestino = new google.maps.Marker({
            position: myLatlngDestino,
            map: map,
            title: "Destino"
          });
          var request = {
            origin: markerOrigen.getPosition(),
            destination: markerDestino.getPosition(),
            travelMode: google.maps.TravelMode.DRIVING
          };
          directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
              directionsDisplay.setDirections(response);
            }
          });
          cont = 0;
        }
      }
    
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    html,
    body,
    #map-canvas {
      height: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <div id="map-canvas"></div>

答案 1 :(得分:0)

尝试使用包装类,这些不是您发送函数的对象。也许你正在发送函数原始数字。

答案 2 :(得分:0)

我认为这是因为这个

var request = { origin:start, destination:end, travelMode: google.maps.TravelMode.DRIVING };

原点和目的地不接受纬度和经度。尝试在地址中转换纬度。使用地理编码来做到这一点。之后,我认为您的代码将起作用

click this if you want to see the sample code