【问题标题】:Google Maps Geocoding a string谷歌地图对字符串进行地理编码
【发布时间】:2014-02-20 02:12:14
【问题描述】:

我在以下位置完成了地理编码示例:

https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

我希望能够在我的代码中对字符串进行地理编码并在该位置放置一个标记,而不是让用户搜索一个位置。

到目前为止我的代码是:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Geocoding service</title>
    <style>
      html, body, #map-canvas 
      {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
   //global variables 
var geocoder;
var map;
var Ireland = "Dublin";

function initialize() 
{
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(53.3496, -6.3263);
  var mapOptions = 
  {
    zoom: 8,
    center: latlng
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  codeAddress();//call the function
}

function codeAddress() 
{
  var address = document.getElementById("Ireland").value;
  geocoder.geocode( {"Ireland":address}, function(results, status) 
  {
    if (status == google.maps.GeocoderStatus.OK) 
    {
      map.setCenter(results[0].geometry.location);//center the map over the result
      //place a marker at the location
      var marker = new google.maps.Marker(
      {
          map: map,
          position: results[0].geometry.location
      });
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
   }
  });
}

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

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

【问题讨论】:

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


    【解决方案1】:

    试试这个。

     <!doctype html>
        <html lang="en">
        <head>
        <meta charset="utf-8">
        <title>Google Map Address</title>
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
        <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 address = "Dublin";
    
        geocoder.geocode( { 'address': address}, function(results, status) {
    
          if (status == google.maps.GeocoderStatus.OK) {
            var latitude = results[0].geometry.location.lat();
            var longitude = results[0].geometry.location.lng();
    
    
            initialize(latitude,longitude);
    
                    } 
    
            }); 
    
    
        function initialize(latitude,longitude) {
            var latlng = new google.maps.LatLng(latitude,longitude);
    
            var myOptions = {
              zoom: 14,
              center: latlng,
              mapTypeId: google.maps.MapTypeId.ROADMAP,
              mapTypeControl: false
            };
            var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
    
            var marker = new google.maps.Marker({
              position: latlng, 
              map: map, 
                title:"location : Dublin"
            }); 
          }
    
    
    </script>
    
        </head>
        <body>
    
        <h2>Google Map Address</h2>
    
        <div id="map_canvas" style="width:710px; height:300px"></div>   
    
                </body>
        </html>
    

    【讨论】:

    • 它会抛出一个初始化不是函数的角度错误。知道我们可以为此做些什么吗?
    【解决方案2】:

    这将更改 codeAddress 函数以获取参数并对该参数进行地理编码。如果您将 Ireland 变量作为参数传递,它将对“Dublin”进行地理编码。

    var Ireland = "Dublin";
    
    function initialize() 
    {
      geocoder = new google.maps.Geocoder();
      var latlng = new google.maps.LatLng(53.3496, -6.3263);
      var mapOptions = 
      {
        zoom: 8,
        center: latlng
      }
      map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
      codeAddress(Ireland);//call the function
    }
    
    function codeAddress(address) 
    {
      geocoder.geocode( {address:address}, function(results, status) 
      {
        if (status == google.maps.GeocoderStatus.OK) 
        {
          map.setCenter(results[0].geometry.location);//center the map over the result
          //place a marker at the location
          var marker = new google.maps.Marker(
          {
              map: map,
              position: results[0].geometry.location
          });
        } else {
          alert('Geocode was not successful for the following reason: ' + status);
       }
      });
    }
    

    代码 sn-p:

    var Ireland = "Dublin";
    
    function initialize() {
      geocoder = new google.maps.Geocoder();
      var latlng = new google.maps.LatLng(53.3496, -6.3263);
      var mapOptions = {
        zoom: 8,
        center: latlng
      }
      map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
      codeAddress(Ireland); //call the function
    }
    
    function codeAddress(address) {
      geocoder.geocode({
        address: address
      }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location); //center the map over the result
          //place a marker at the location
          var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
          });
        } else {
          alert('Geocode was not successful for the following reason: ' + status);
        }
      });
    }
    html,
    body,
    #map-canvas {
      height: 100%;
      width: 100%;
      padding: 0;
      margin: 0;
    }
    <div id="map-canvas"></div>
    <!-- Replace the value of the key parameter with your own API key. -->
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initialize" async defer></script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      相关资源
      最近更新 更多