简体   繁体   English

Google Maps:从经过地理编码的邮政编码中获取经纬度

[英]Google Maps: get lat/lng from geocoded postcode

I am currently plotting multiple points on a map, using addresses from an object. 我目前正在使用对象的地址在地图上绘制多个点。 The program loops over the object, geocodes the address, and plots a marker for each location. 程序在对象上循环,对地址进行地址geocodes ,并为每个位置绘制一个标记。

The problem I am having is when a user clicks on a place in a list, the map is to pan to that location. 我遇到的问题是,当用户单击列表中的某个位置时,地图将平移到该位置。 The API has a panTo() function that accepts lat, lng values, but the results, ie results[0].geometry.location , from the geocode function are not available outside of it. 该API具有panTo()函数,该函数接受lat, lng值,但是地理编码函数的结果(即results[0].geometry.location )在其外部不可用。

Question

How do I somehow retrieve the lat, lng from the results, maybe append them to the existing data object, and use them outside the function, so I am able to use them in the panTo() function? 我如何以某种方式从结果中检索lat, lng ,也许将其附加到现有数据对象中,并在函数外使用它们,以便能够在panTo()函数中使用它们?

[The lat/lng values are output in html data attributes] [经/纬度值在html data属性中输出]

Click handler 点击处理程序

$('.place').on('click', function() {
    $this = $(this);
    lat = $this.data('lat');
    lng = $this.data('lng');

    map.panTo(new google.maps.LatLng(lat, lng));
});

Data 数据

var locations = [
    {   
      id: 'place1',
      postcode: 'B1 1AA'
    },
    {   
     id: 'place2',
      postcode: 'CB9 8PU'
    }
];

Code

for (i = 0; i < locations.length; i++) {

  geocoder.geocode({'address': locations[i].postcode}, function(results, status) {
      if(status == google.maps.GeocoderStatus.OK) {

          marker = new google.maps.Marker({
              position: results[0].geometry.location,
              map: map
          });

      } else {
          console.log('Geocode was not successful ' + status);
      }

  }); // end geocode


    $(places).append(
        '<li class="place" data-id="'+locations[i].id+'"  data-lat="<!--lat to go here-->" data-lng="<!--lng to go here-->">'+
            '<div class="place-wrap">'+
                '<h2>'+locations[i].name+'</h2>'+
                '<p class="territory">'+locations[i].territory+'<p>'+
            '</div>'+
        '</li>'
    );

} // end for

Give this a go. 快去 The IIFE is there to enclose the index so you don't keep hitting the last index of the locations array due to the asynchronous nature of geocode. IIFE用来封装索引,因此,由于地理编码的异步特性,您不必一直访问locations数组的最后一个索引。

  for (i = 0, l = locations.length; i < l; i++) {

    (function(i) { // index passed in as parameter

      geocoder.geocode({ address: locations[i].postcode }, function(results, status) {

        if (status == google.maps.GeocoderStatus.OK) {

          marker = new google.maps.Marker({
            position: results[0].geometry.location,
            map: map
          });

          $('#places').append(
            '<li class="place" data-id="' + locations[i].id + '"  data-lat="' + results[0].geometry.location.lat() + '" data-lng="' + results[0].geometry.location.lng() + '">' +
            '<div class="place-wrap">' +
            '<h2>' + locations[i].name + '</h2>' +
            '<p class="territory">' + locations[i].territory + '<p>'+
            '</div>' +
            '</li>'
          );

        } else {
            console.log('Geocode was not successful ' + status);
        }

      });

    }(i)); // index passed in to IIFE

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM