使用Javascript和Google地图,在地图上按照经度和纬度坐标显示标记。

3

你好,我有一段 JavaScript 代码可以根据地址在地图上显示图钉。这些地址是从一个数组中读取的,数组结构如下:

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"];

展示带有标记的地图所使用的代码如下:
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]]

那我该如何更改我的代码,使用这些坐标而非地址位置呢?

谢谢。

1个回答

2

试试这个:

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对象来代替对地址进行地理编码。


抱歉,我对谷歌地图不熟悉,我想更改实际代码,但我不知道如何在我的代码中使用你的代码。 - OussamaLord
你能展示一下你在哪里使用了 var locations 吗? - pascal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接