javascript - 状态零结果谷歌地图地理编码器状态

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

我们需要在 Google map 中的某些点之间绘制方向。我们尝试过,但 DirectionsStatus 出现“零结果”。我们这样尝试

 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

最佳答案

从输出来看,您的 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}]

希望这有帮助

关于javascript - 状态零结果谷歌地图地理编码器状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29114798/

相关文章:

swift - 当应用程序在后台时如何使用计时器

javascript - 参数无效 - Edge 浏览器中的 Google map 错误

android cordova : cordova. exec() 未定义不是一个函数

Javascript:Phoenix channel 立即进入错误状态

javascript - 如何防止我的平滑滚动 jquery 函数修改我的 div 元素的内容?

javascript - 将 undefined 分配给构造函数中的属性

android - 人行横道存储限制

javascript - 使用 js 加载 css 样式表来更新已渲染网页的样式

java - 如何在 map 标记(汽车)周围放置进度条

css - 在 Jquery Mobile 中,如何在 iPad 的手机间隙应用程序中禁用放大镜?