代码之家  ›  专栏  ›  技术社区  ›  rob.m

如何在dragend和google maps自动完成后获得国家名称?

  •  -1
  • rob.m  · 技术社区  · 6 年前

    这是我使用的代码:

      defaultLatLong = {
        lat: 45.4667971, 
        lng: 9.1904984
      };
    
      var map = new google.maps.Map(document.getElementById('map-canvas_1'), {
        center: defaultLatLong,
        zoom: 2,
        mapTypeId: 'roadmap',
        disableDefaultUI: true,
        zoomControl: true
      });
    
      var input = document.getElementById('location-text-box');
    
      var autocomplete = new google.maps.places.Autocomplete(input);
    
      autocomplete.bindTo('bounds', map);
    
      var marker = new google.maps.Marker({
        map: map,
        position: defaultLatLong,
        draggable: true,
        clickable: true
      });
    
      google.maps.event.addListener(marker, 'dragend', function(marker) {
    
        locationsArray.push(country);
        console.log(country);
        var latLng = marker.latLng;
        currentLatitude = latLng.lat();
        currentLongitude = latLng.lng();
        var latlng = {
          lat: currentLatitude,
          lng: currentLongitude
        };
        var geocoder = new google.maps.Geocoder;
        geocoder.geocode({
          'location': latlng
        }, function(results, status) {
          if (status === 'OK') {
            if (results[0]) {
              input.value = results[0].formatted_address;
            } else {
              window.alert('No results found');
            }
          } else {
            window.alert('Geocoder failed due to: ' + status);
          }
        });
      });
    
      autocomplete.addListener('place_changed', function() {
        var place = autocomplete.getPlace();
        if (!place.geometry) {
          return;
        }
        if (place.geometry.viewport) {
          map.fitBounds(place.geometry.viewport);
        } else {
          map.setCenter(place.geometry.location);
        }
        marker.setPosition(place.geometry.location);
        currentLatitude = place.geometry.location.lat();
        currentLongitude = place.geometry.location.lng();
      });
    

    我尝试了以下方法,因为我曾将它与另一个GoogleMaps代码一起使用,但这不起作用,因为它引发了一个错误:

      google.maps.event.addListener(marker, 'dragend', function(marker) {
        var country = filtered_array.length ? filtered_array[0].long_name: "";
    

    错误:

    未捕获引用错误:未定义筛选的_数组

    理想情况下,我希望获得所有信息:

    地址、国家名称等。

    1 回复  |  直到 6 年前
        1
  •  0
  •   rob.m    6 年前

    有我自己的答案。因为我两个都用 autocomplete dragend 我们需要 loop 回调后的结果:

    所以在问题中为dragend发布的代码中查找 input.value = results[0].formatted_address; 做:

    var arrAddress = results[0].address_components;
    $.each(arrAddress, function (i, address_component) {
      console.log(address_component.long_name); // here's your town name
    });
    

    然后自动完成查找 currentLongitude = place.geometry.location.lng(); 然后做

    var arrAddress = place.address_components;
    $.each(arrAddress, function (i, address_component) {
      console.log(address_component.long_name);
    });
    

    现在地址的所有详细信息都可以保存在不同的 inputs vars