Javascript 谷歌地图,按经度和纬度坐标显示图钉

标签 javascript google-maps

<分区>

你好,我有一个 Javascript 代码,它显示了一个带有图钉的 map ,使用那里的位置作为地址。 地址是从数组中读取的,数组结构是这样的:

var locations = ["1915 Edgewater Drive,Orlando,FL", "5555 Kirkman Rd.,Orlando,FL", "4965 Oak Ridge Road,Orlando,FL", "3011 E Colonial Dr,Orlando,FL", "300 Park Avenue,Winter Park,FL"];

用于显示带有图钉的 map 的代码如下:

function initialize() {
  //Planned direction display
  directionsDisplay = new google.maps.DirectionsRenderer({
      polylineOptions : {
          strokeColor : 'brown',
          strokeOpacity : 0.7,
          strokeWeight : 3
      },
      suppressMarkers : true,
      suppressPolylines : true
  });

  //actual direction display
  directionsDisplayActual = new google.maps.DirectionsRenderer({
      polylineOptions : {
          strokeColor : 'brown',
          strokeOpacity : 0.7,
          strokeWeight : 3
      },
      suppressMarkers : true,
      suppressPolylines : true
  });

    geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': locations[0] }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {

        var chicago = new google.maps.LatLng(results[0].geometry.location.ob, results[0].geometry.location.pb);
          var mapOptions = {
                zoom: 6,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                center: chicago
          }
          map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
          map_actual = new google.maps.Map(document.getElementById('map-actual'), mapOptions);
          calcRoute();
        }
    });

}

现在我没有使用带有地址的位置,而是有一系列像这样的经度和纬度坐标:

var coordinates = [[28.378929,-80.6006008],[28.378628084853,-80.600730804958],[28.378956475407,-80.600553605881],[28.378929693408,-80.60055175679],[28.378785540514,-80.60065325285],[28.378935283517,-80.600646565422],[28.378950753943,-80.600504807166]]

那么如何更改我的代码以使用这些坐标而不是位置地址?

谢谢。

最佳答案

试试这个:

var coordinates = [[28.378929,-80.6006008],[28.378628084853,-80.600730804958],[28.378956475407,-80.600553605881],[28.378929693408,-80.60055175679],[28.378785540514,-80.60065325285],[28.378935283517,-80.600646565422],[28.378950753943,-80.600504807166]];
// loop through your array of coordinates
for(var i = 0; i < coordinates.length; i++){
    // create a new marker and add it to the map
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(coordinates[i][0], coordinates[i][1]),
        map: map
    });
}

编辑:您使用 LatLng对象而不是对地址进行地理编码

关于Javascript 谷歌地图,按经度和纬度坐标显示图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20678347/

相关文章:

javascript - 带有 Google Maps V3 的矢量切片

javascript - 在 IE8 中打印谷歌地图打印和使用 'page-break-before' 时出现问题

javascript - “ float 层”被困在 HTML 表格下方

javascript - 动态创建的 iframe 内容为空

javascript - 如何减少分组条形图中条形之间的填充?

google-maps - 向 Angular2 Google map 添加样式

html - 如何在 cordova webview 之外打开 <a href> 链接(在 Safari 或 Google map 中)?

linux - 自定义谷歌地图应用程序,地标在 map 上以一定间隔给定的位置移动——可能吗?

javascript - 使用 let 初始化的函数在使用 this 访问时不会被替换

javascript - webgl简单图像效果