【问题标题】:How to get reverse geocoding value into an input field?如何将反向地理编码值放入输入字段?
【发布时间】:2019-02-17 15:25:59
【问题描述】:

google 为reverse geocoding coordinates into full address 提供的示例代码很好,使用this stack question 上的回复时结果相同

问题是我不希望根据示例将它们放在信息框中,但我想更新我的输入字段,这是我现在使用的代码,删除了反向地理编码,因为我无法转换它变成一个输入值:

<p id="addressHelper" ></p>
<div id="map"></div>

var myMarker = new google.maps.Marker({
 position: new google.maps.LatLng(47.651968, 9.478485),
 draggable: true
});

google.maps.event.addListener(myMarker, 'dragend', function (evt) {
 document.getElementById('addressHelper').innerHTML = '<p>Marker dropped: Current Lat: ' + evt.latLng.lat().toFixed(3) + ' Current Lng: ' + evt.latLng.lng().toFixed(3) + '</p>';
});

google.maps.event.addListener(myMarker, 'dragstart', function (evt) {
  document.getElementById('addressHelper').innerHTML = '<p>Currently dragging marker...</p>';
});

map.setCenter(myMarker.position);
myMarker.setMap(map);

【问题讨论】:

  • 没有解释,只是一个简单的、干巴巴的、不赞成的投票。甜的。谢谢

标签: javascript jquery google-maps


【解决方案1】:

在玩了一会儿并阅读了其他 tuts 之后,这是我的解决方案:

<input type="text" id="location-text-box" name="address" placeholder="Your location..." required="">
<div id="map"></div>


    window.onload = function () {
      var myLatlng = new google.maps.LatLng(-25.363882, 131.044922);
      var mapOptions = {
        center: myLatlng,
        zoom: 1,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableDefaultUI: true,
        zoomControl: true
      };

      var map = new google.maps.Map(document.getElementById("map"), mapOptions);

      var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        draggable:true,
        title:"Drag me!"
      });

      google.maps.event.addListener(marker, 'dragend', function (e) {
        var latlng = new google.maps.LatLng(e.latLng.lat(), e.latLng.lng());
        var geocoder = geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'latLng': latlng }, function (results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
              $("#location-text-box").attr("value", results[1].formatted_address);
            }
          }
        });
      });
    }

【讨论】:

    猜你喜欢
    • 2016-07-14
    • 2019-01-11
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多