【问题标题】:Showing path on google map from json data从 json 数据在谷歌地图上显示路径
【发布时间】:2013-07-27 13:05:12
【问题描述】:

这里我有标记作为 Json 对象响应,这些标记正在谷歌地图上显示。在这里,我想在这些标记之间绘制路径,但萤火虫显示(value.Latitude)的错误无效值。这里的值是我的 JSON 对象。

var request = {
                    origin:value.Latitude, 
                    destination:value.Longitude,
                    travelMode: google.maps.DirectionsTravelMode.DRIVING
                };

这是我的整个脚本

  <script type="text/javascript"
    src="http://maps.google.com/maps/api/js?sensor=false">
 </script>

 <style type="text/css">
  #map_canvas {
  height: 500px;
  width: 800px;
  position: static;
  top: 0px;
  left: 100px;
}
</style>



<script>
      var directionDisplay;
       var directionsService = new google.maps.DirectionsService();

     function initialize() {
var latlng = new google.maps.LatLng(18.5236, 73.8478);
directionsDisplay = new google.maps.DirectionsRenderer();

var myOptions = {
    zoom: 8,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    streetViewControl: false,
    mapTypeControl: false
};

    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
  directionsDisplay.setMap(map);

$(document).ready(function () {
    $("#btnSubmit").click(function () {
        var Source = $("#Source").val();
        var Destination = $("#Destination").val();
        authenticate(Source, Destination);
    });
});

function authenticate(Source,Destination) {
   $('#somediv').empty();

   var table = $('<table/>').appendTo($('#somediv'));
   $.ajax
    ({
      type: 'GET',
        url: 'REST/WebService/GetLogin',
        dataType: 'json',
        data:{'Source': Source, 'Destination': Destination },

        error:function()
        {
            alert("unsuccessfull");
        },
        success:function(feeds)
        {   
            alert(feeds.length);

            $.each(feeds, function (index, value) {

               new google.maps.Marker({
                        position: new google.maps.LatLng(value.Latitude,value.Longitude),
                        map: map,
                        title: "Marker Placing"
              });

                var request = {
                    origin:value.Latitude, 
                    destination:value.Longitude,
                    travelMode: google.maps.DirectionsTravelMode.DRIVING
                };

               directionsService.route(request, function(response, status) {
                    if (status == google.maps.DirectionsStatus.OK) {
                        directionsDisplay.setDirections(response);
                    }
               });  
            });
                //alert(feeds.toSource());
        }

     });
  }
}
</script>

【问题讨论】:

  • origin:value.Latitudedestination:value.Longitude 的值是多少?根据他们的姓名以及您如何使用他们创建 google.maps.Marker,路线请求似乎不正确,他们需要是地址字符串或 google.maps.LatLng 对象,而不仅仅是数字。

标签: jquery json rest google-maps-api-3


【解决方案1】:

showing error invalid value of (value.Latitude).

value.Latitude 是一个数字。要将其传递到 DirectionsService 请求中,需要将其转换为 google.maps.LatLng 对象(其构造函数接受 2 个数字参数)或作为可地理编码地址的字符串。

From the documentation:

origin      | LatLng|string | Location of origin. This can be specified as either a string to be geocoded or a LatLng. Required.
destination | LatLng|string | Location of destination. This can be specified as either a string to be geocoded or a LatLng. Required.

【讨论】:

  • 那我该怎么做呢?我被困在这里,因为我正在尝试一些随机的东西。
  • 源和目标坐标是多少?我只看到一个标记/一组坐标。用它们创建 google.maps.LatLng 对象并将它们传递给 DirectionsRequest。
  • 源坐标和目标坐标是经纬度值形式的 JSON 对象。我到处都看到开始和结束作为字符串。如果我希望这些是纬度和经度值怎么办。我不知道如何转换这些并将其传递给 google.maps.LatLng()?
  • 在这里我可以转换为 var request = { origin:new google.maps.LatLng(value.Latitude,value.Longitude),destination:new google.maps.LatLng(value.Latitude, value.Longitude),travelMode: google.maps.DirectionsTravelMode.DRIVING };.但是值正在从 json 对象中折叠,我希望它们以某种方式分开?
  • 你是什么意思“值从 json 对象中被折叠?那些是相同的位置,你真的想这样做吗?如果不是,你需要以某种方式使这两个位置唯一。跨度>
猜你喜欢
  • 1970-01-01
  • 2011-01-23
  • 1970-01-01
  • 2014-01-25
  • 2018-06-16
  • 2016-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多