【问题标题】:Problems with map: draw route with 2 coordinates地图问题:用 2 个坐标绘制路线
【发布时间】:2016-05-11 14:05:12
【问题描述】:

对不起,如果这是以前已经问过的问题。我想加载一个带有 2 个标记的地图,起点和终点。建立 2 个标记时,我绘制了要遵循的路径(模式:驱动器)。

当我选择 2 个标记时,我收到此错误:“Uncaught InvalidValueError: in property origin: not a string; and not a LatLng or LatLngLiteral: not an Object” 请帮帮我!

<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>

【问题讨论】:

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


    【解决方案1】:

    我收到 your code: Uncaught 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 在第二次点击时超出范围

    最简单的解决方法是将标记设为全局,然后在运行路线服务时获取标记的位置:

            var request = {
                origin: markerOrigen.getPosition(),
                destination: markerDestino.getPosition(),
                travelMode: google.maps.TravelMode.DRIVING
            };
    
    • 您还需要将 DirectionsService 的调用移到 click 事件处理函数的 else 中,因为在您同时拥有起点和终点之前它不会起作用。

      } 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,因为您没有初始化方向显示对象。

    一旦我定义它有效。

    working fiddle

    代码 sn-p:

    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>

    【讨论】:

    • 非常感谢!你一直很有帮助! (:
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我想是因为这个

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

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

      click this if you want to see the sample code

      【讨论】:

      • 路线服务可以接受表示地址的字符串或 google.maps.LatLng 指定 DirectionsRequest 的起点和终点属性的坐标。
      猜你喜欢
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      相关资源
      最近更新 更多