【问题标题】:status zero results google maps GeocoderStatus状态零结果谷歌地图地理编码器状态
【发布时间】:2015-05-20 19:24:38
【问题描述】:

我们需要在 Google 地图中的某些点之间绘制方向。我们尝试过,但 DirectionsStatus 来自"zero results"。我们尝试过这样

 function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  var chicago = new google.maps.LatLng(41.850033, -87.6500523);
  var mapOptions = {
    zoom: 6,
    center: chicago
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  directionsDisplay.setMap(map);
}

function calcRoute() {
debugger;

  var waypts = [];
  var checkboxArray = document.getElementById('waypoints');
  for (var i = 0; i < myjson.length; i++) {
   var data = myjson[i];



      waypts.push({
            location: new google.maps.LatLng(parseFloat(myjson[i].Lattitude), parseFloat(myjson[i].Longitude)),
            stopover: true
        });

  }
  debugger;
  console.log(waypts);
 var request = {

      origin:waypts[0].location,
      destination:waypts[waypts.length-1].location,
      waypoints: waypts,
      optimizeWaypoints: true,
      travelMode: google.maps.TravelMode.DRIVING
  };
debugger;
  directionsService.route(request, function(response, status) {
  debugger;
    if (status == google.maps.DirectionsStatus.OK) {
     debugger;
      directionsDisplay.setDirections(response);
      var route = response.routes[0];
    }
  });
}

我们需要状态码"OK"。请帮帮我。告诉我我的代码出了什么问题。我们有 json,然后 JSON 数据传递给 Myjson 变量。calcRoute 按钮操作中的方法调用。

当我们打印航点时

Array[6]0: Objectlocation: kfD: 78.54940429999999k: 17.4211137__proto__: kfstopover: true__proto__: Object1: Objectlocation: kfstopover: true__proto__: Object2: Objectlocation: kfstopover: true__proto__: Object3: Object4: Object5: Objectlength: 6

【问题讨论】:

  • 当您提醒waypts(或console.log())时,它会是什么样子?
  • @duncan waypts like this 0: Object location: kf D: 78.54940429999999 k: 17.4211137 proto: kf stopover: true proto: Object 1:对象 2:对象 3:对象
  • 呃...看起来不像一个数组,但很难从该评论中看出。如果您执行 console.log() ,您能否准确地更新您的答案?
  • @duncan 是的,我们更新代码开玩笑我们打印航点

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


【解决方案1】:

在我的例子中,数据来自 HTML li 元素,但它以字符串的形式出现,即

coordsData = {
    lat: {
        value: "19.4921383",
        someOtherProperties: {}
    },
    lng: {
        value: "-99.04651999999999",
        someOtherProperties: {}
    }
}

所以,我必须将这些值转换为:

var myCoords = new google.maps.LatLng(parseFloat(coordsData.lat.value), parseFloat(coordsData.lng.value))

以防万一有人遇到同样的问题。

【讨论】:

    【解决方案2】:

    从输出来看,您的 myjson(数组?)似乎有问题。

    无论如何,当我尝试复制它时,它可以工作。

      //from how you use your myjson var, I would assume it looks something like this:
      var myjson = [
          { Lattitude: 37.4184, Longitude: -122.0880 },
          { Lattitude: 37.7833, Longitude: -122.4167 },
          { Lattitude: 38.5539, Longitude: -121.7381 }
      ]
    
      var waypts = [];
      for (var i = 0; i < myjson.length; i++) {
      var data = myjson[i];
          waypts.push({
                location: new google.maps.LatLng(parseFloat(myjson[i].Lattitude), parseFloat(myjson[i].Longitude)),
                stopover: true
            });
      }
      console.log(JSON.stringify(waypts));
    
      var request = {
            origin:waypts[0].location,
            destination:waypts[waypts.length-1].location,
            waypoints: waypts.slice(1, waypts.length-1),
            //the example in the documentation does not include the start & end points
            //that might be a problem https://developers.google.com/maps/documentation/javascript/directions#DirectionsRequests
            optimizeWaypoints: true,
            travelMode: google.maps.TravelMode.DRIVING
      };
    
      directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
        }
      });
    

    打印航路点时

    [{"location":{"k":37.4184,"D":-122.08799999999997},"stopover":true},{"location":{"k":37.7833,"D":-122.41669999999999},"stopover":true},{"location":{"k":38.5539,"D":-121.73810000000003},"stopover":true}]
    

    希望有帮助

    【讨论】:

      猜你喜欢
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多