如何使用变量来定义新的google.maps.LatLng()

How do I use variable to define new google.maps.LatLng()

本文关键字:maps google LatLng 何使用 变量 定义      更新时间:2023-09-26

我已经读了,读了,读了,但我不能弄清楚这个。我有一个lat数组和一个lng数组。我需要能够将值从数组传递到新的google.maps.LatLng()。看起来这是可行的:

new google.maps.LatLng(lats[0], lngs[0])

但它没有。我想不出该怎样做这件事。帮助吗?

下面是创建数组的方法:
    <script type="text/javascript">
var lats = [];
var lngs = [];
  function initialize() {
var geo = new google.maps.Geocoder;
  var address = "<?php echo $arrayAddress; ?>";
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {              
    lats.push(results[0].geometry.location.lat());
    lngs.push(results[0].geometry.location.lng());
} else {
    alert("Geocode was not successful for the following reason: " + status);
}
});
  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>

如果我console.log(lat[0]),我得到预期的结果。

这里我要创建new。google。mapslatlng ():

<script type="text/javascript">
var map;
function initialize() {
    var mapOptions = {
    zoom: 8,
    center: new google.maps.LatLng(lats[0], lngs[0])
    };
    map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
 }
 google.maps.event.addDomListener(window, 'load', initialize);
 </script>

这是在wordpress的循环之外。地理编码器部分在循环内。我正在对循环中所有帖子的地址进行地理编码,并将坐标移动到该数组中,然后将坐标数组作为标记放置在循环之外的地图上。

 var locat = new google.maps.LatLng(34,74);
            geocoder = new google.maps.Geocoder();
            var mapOptions = {
                zoom: 11,
                center: locat,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById(divID), mapOptions);