谷歌api反向地理编码

时间:2015-09-05 16:36:09

标签: javascript forms google-maps google-maps-api-3

我的网站上有谷歌地图。用户可以拖动标记,地图会将lat和lon输入到我的表格中。见下面的代码。我希望能够从lat和lon获取地址并将其放入“locationbox”中的表单中。

    <script src="multi_step_form.js"></script>

    <script src="https://maps.googleapis.com/maps/api/js?callback=initMap" async defer></script>
    <script type="text/javascript">
  var map;
  function initialize() {
  var myLatlng = new google.maps.LatLng(49.25302534866034,-102.04825518471148);
  var myOptions = {
     zoom: 3,
     center: myLatlng,
     mapTypeId: google.maps.MapTypeId.HYBRID
     }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
  var marker = new google.maps.Marker({
  draggable: true,
  position: myLatlng, 
  map: map,
  title: "Your location"
  });
    google.maps.event.addListener(marker, 'dragend', function (event) {
    document.getElementById("latbox").value = this.getPosition().lat();
    document.getElementById("lngbox").value = this.getPosition().lng();
    });
  }
</script> 

我有另外一些代码来查找我从https://203luv.wordpress.com/2011/10/21/google-maps-javascript-v3-api-how-to-get-address-from-coordinates-latitude-longitude/获得的地址,但我无法弄清楚如何将它们混合在一起。

    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">

var geocoder = new google.maps.Geocoder();

var lat = "12.1234";

var long = "98.7654";

var latlng = new google.maps.LatLng(sLat, sLong);

geocoder.geocode({"latLng":latlng},function(data,status){

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

var add = data[1].formatted_address; //this is the full address

alert(add);

for(var i=0; i<data[1].address_components.length; i++){

if(results[1].address_components[i].types[0] == "administrative_area_level_1"){

alert(results[1].address_components[i].short_name); 

}

}

}

})

我的html表单看起来像这样

      <div id="map_canvas" style="width: 450px; height: 450px; background-color: Black;"></div>
  <div id="latlong">
    <p><input size="20" type="text" id="latbox" name="lat" placeholder="Drag the marker on the map or type in the latitude"></p>
    <p><input size="20" type="text" id="lngbox" name="lon"  placeholder="Drag the marker on the map  or type in the longitude"></p>
  </div>
<input class="text_field" id="locationbox" name="location" placeholder="Location" type="text" >

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

我建议在dragend事件监听器函数中调用反向地理编码器:

google.maps.event.addListener(marker, 'dragend', function (event) {
    document.getElementById("latbox").value = this.getPosition().lat();
    document.getElementById("lngbox").value = this.getPosition().lng();
    var latlng = this.getPosition();
    geocoder.geocode({
        "latLng": latlng
    }, function (data, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var add = data[1].formatted_address; //this is the full address
            // alert(add);
            for (var i = 0; i < data[1].address_components.length; i++) {
                if (data[1].address_components[i].types[0] == "administrative_area_level_1") {
                    document.getElementById('locationbox').value = data[1].address_components[i].short_name;
                }
            }
        }
    });
});

proof of concept fiddle

代码段

&#13;
&#13;
var map;

function initialize() {
  var geocoder = new google.maps.Geocoder();
  var myLatlng = new google.maps.LatLng(49.25302534866034, -102.04825518471148);
  var myOptions = {
    zoom: 3,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.HYBRID
  };
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  var marker = new google.maps.Marker({
    draggable: true,
    position: myLatlng,
    map: map,
    title: "Your location"
  });
  google.maps.event.addListener(marker, 'dragend', function(event) {
    document.getElementById("latbox").value = this.getPosition().lat();
    document.getElementById("lngbox").value = this.getPosition().lng();

    var latlng = this.getPosition();

    geocoder.geocode({
      "latLng": latlng
    }, function(data, status) {

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

        var add = data[1].formatted_address; //this is the full address

        // alert(add);

        for (var i = 0; i < data[1].address_components.length; i++) {

          if (data[1].address_components[i].types[0] == "administrative_area_level_1") {

            document.getElementById('locationbox').value = data[1].address_components[i].short_name;

          }
        }
      }
    });
  });
}
google.maps.event.addDomListener(window, 'load', initialize);
&#13;
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas" style="width: 450px; height: 450px; background-color: Black;"></div>
<div id="latlong">
  <p>
    <input size="20" type="text" id="latbox" name="lat" placeholder="Drag the marker on the map or type in the latitude">
  </p>
  <p>
    <input size="20" type="text" id="lngbox" name="lon" placeholder="Drag the marker on the map  or type in the longitude">
  </p>
</div>
<input class="text_field" id="locationbox" name="location" placeholder="Location" type="text">
&#13;
&#13;
&#13;