【问题标题】:Dynamic reverse geocoding from map center on Google Maps?谷歌地图上地图中心的动态反向地理编码?
【发布时间】:2016-10-16 21:46:51
【问题描述】:

对于我正在进行的一项民意调查,我有这张地图,它可以对您的位置进行地理编码并将地图居中放置在该特定地点。通过 jquery,我可以同时获得经度和纬度,以便从地图中心所在的任何位置填写投票字段。

我想知道的是如何将地址添加为第三个投票字段,就像坐标一样,以便在我的地图中心发生变化时动态获取它。我在这篇文章中看到了类似的东西,他们从拖动的标记中动态获取地址: How to get the formatted address from a dragged marker in Google Version Maps

在这里你可以看到我的工作地图:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
  <head>
    <title>Geolocation Flecker</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        width: 100%;
        margin: 0px;        
        padding: 0px
      }
 #map-canvas {
    position: relative;
}

#map-canvas:after {
    width: 66px;
    height: 66px;
    display: block;
    content: ' ';
    position: absolute;
    top: 50%; left: 50%;
    margin: -66px 0 0 -44px;
    background: url('https://storage.googleapis.com/operations_poligone/iconos/Stick01.png');
    background-size: 88px 66px; 
    pointer-events: none; 
}
      
    </style>

    <script src="https://maps.googleapis.com/maps/api/js?V=3.exp&key=AIzaSyDgwY_XXwSE2v-ZCWziaJ6qjuTuiBJck9A
&libraries=visualization&libraries=places"></script>

        <script
src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

    <script>
      
var map;

function initialize() {

 var mapOptions = {
                    zoom: 11,
                    scrollwheel: true,
                    panControl: true,
                    zoomControl: true,
                    scaleControl: false,
                    disableDefaultUI: true,
                    center: new google.maps.LatLng(19.038235, -98.219530),
                    mapTypeControlOptions: {
                        mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']
                    }
  };
  
map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions); 
  
  
      //Retrive the center location
      google.maps.event.addListener(map, "center_changed", function() {
        var lat = map.getCenter().lat();
        var lng = map.getCenter().lng();
        $('#lat').val(lat);
        $('#lng').val(lng); 
      });  
  
  
  // Try HTML5 geolocation
  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);
  document.getElementById('lat').value = position.coords.latitude;
  document.getElementById('lng').value = position.coords.longitude;
      var marker = new google.maps.Marker({
        map: map,
        position: pos,
        title: 'Ubicación GPS',
     icon:'https://storage.googleapis.com/operations_poligone/iconos/GPS_P06.png',
        animation: google.maps.Animation.BOUNCE
      });

      map.setZoom(16);
      map.setCenter(pos);
    }, function() {
      handleNoGeolocation(true);
    });
  } else {
    // Browser doesn't support Geolocation
    handleNoGeolocation(false);
  }
}

function handleNoGeolocation(errorFlag) {
  if (errorFlag) {
    var content = 'Turn GPS on';
  } else {
    var content = 'Try with your fingers';
  }

  var options = {
    map: map,
    position: new google.maps.LatLng(19.038235, -98.219530),
    content: content,
    pixelOffset: new google.maps.Size(0,-30)
  };

  var infowindow = new google.maps.InfoWindow(options);
  map.setCenter(options.position);
      
 }    

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
        <input id="lat"/>
        <input id="lng"/>
        <input id="add"/>
  </body>
</html>

【问题讨论】:

    标签: javascript jquery google-maps-api-3 geolocation geocoding


    【解决方案1】:

    the linked question 添加geocodePosition 函数,更改它以更新您的&lt;input&gt;。在“center_changed”函数中调用它。

    function geocodePosition(pos) {
      geocoder.geocode({
        latLng: pos
      }, function(responses) {
        if (responses && responses.length > 0) {
          $('#add').val(responses[0].formatted_address);
        } else {
          $('#add').val('Cannot determine address at this location.');
        }
      });
    }
    
    //Retrive the center location
    google.maps.event.addListener(map, "center_changed", function() {
      var lat = map.getCenter().lat();
      var lng = map.getCenter().lng();
      $('#lat').val(lat);
      $('#lng').val(lng);
      geocodePosition(map.getCenter())
    });
    

    var map;
    var geocoder;
    
    function initialize() {
      geocoder = new google.maps.Geocoder();
      var mapOptions = {
        zoom: 11,
        scrollwheel: true,
        panControl: true,
        zoomControl: true,
        scaleControl: false,
        disableDefaultUI: true,
        center: new google.maps.LatLng(19.038235, -98.219530),
        mapTypeControlOptions: {
          mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']
        }
      };
    
      map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);
    
    
      //Retrive the center location
      google.maps.event.addListener(map, "center_changed", function() {
        var lat = map.getCenter().lat();
        var lng = map.getCenter().lng();
        $('#lat').val(lat);
        $('#lng').val(lng);
        geocodePosition(map.getCenter())
      });
    
    
      // Try HTML5 geolocation
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
          var pos = new google.maps.LatLng(position.coords.latitude,
            position.coords.longitude);
          document.getElementById('lat').value = position.coords.latitude;
          document.getElementById('lng').value = position.coords.longitude;
          var marker = new google.maps.Marker({
            map: map,
            position: pos,
            title: 'Ubicación GPS',
            icon: 'https://storage.googleapis.com/operations_poligone/iconos/GPS_P06.png',
            animation: google.maps.Animation.BOUNCE
          });
    
          map.setZoom(16);
          map.setCenter(pos);
        }, function() {
          handleNoGeolocation(true);
        });
      } else {
        // Browser doesn't support Geolocation
        handleNoGeolocation(false);
      }
    }
    
    function handleNoGeolocation(errorFlag) {
      if (errorFlag) {
        var content = 'Turn GPS on';
      } else {
        var content = 'Try with your fingers';
      }
    
      var options = {
        map: map,
        position: new google.maps.LatLng(19.038235, -98.219530),
        content: content,
        pixelOffset: new google.maps.Size(0, -30)
      };
    
      var infowindow = new google.maps.InfoWindow(options);
      map.setCenter(options.position);
    
    }
    
    function geocodePosition(pos) {
      geocoder.geocode({
        latLng: pos
      }, function(responses) {
        if (responses && responses.length > 0) {
          $('#add').val(responses[0].formatted_address);
        } else {
          $('#add').val('Cannot determine address at this location.');
        }
      });
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    html,
    body,
    #map-canvas {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    #map-canvas {
      position: relative;
    }
    #map-canvas:after {
      width: 66px;
      height: 66px;
      display: block;
      content: ' ';
      position: absolute;
      top: 50%;
      left: 50%;
      margin: -66px 0 0 -44px;
      background: url('https://storage.googleapis.com/operations_poligone/iconos/Stick01.png');
      background-size: 88px 66px;
      pointer-events: none;
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div id="map-canvas"></div>
    <input id="lat" />
    <input id="lng" />
    <input id="add" />

    【讨论】:

    • 它工作正常,但我认为缺少一些东西,当移动地图(平移)时,大多数时候地址输入说无法确定地址我需要一个 更新指令就像在参考示例中一样?像这样的东西:google.maps.event.addListener(marker, 'dragend', function() { updateMarkerStatus('Drag ended'); geocodePosition(marker.getPosition()); });@geocodezip
    • 如果反向地理编码器找不到结果,这就是该示例中的代码所做的。我看到的大多数地方都在公园中间(看到这一点我并不感到惊讶。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多