简体   繁体   English

Google地图地理编码(GLatLng地址)

[英]Google Maps geocoding (address to GLatLng)

I am trying to draw a geodesic polyline with Google Maps JavaScript API from two address points. 我正在尝试从两个地址点使用Google Maps JavaScript API绘制测地线多段线。

var polyOptions = {geodesic:true};
var polyline = new GPolyline([
    new GLatLng(),
    new GLatLng()
  ], "#f36c25", 5, 0.8, polyOptions
);

map.addOverlay(polyline);

if (GBrowserIsCompatible()) {
  map.addOverlay(polyline);
}

Could someone tell me how can I dynamically geocode an address into GLatLng coordinates? 有人能告诉我如何动态地将地址编码为GLatLng坐标? I am a little confused after reading Google's API documentation . 阅读Google的API文档后,我有点困惑。

You may want to check out the following example. 您可能需要查看以下示例。 First it tries to geocode address1 . 首先,它会尝试进行地理编码address1 If it succeeds, it tries to geocode address2 . 如果成功,它会尝试对address2进行地理编码。 If both succeed, it plots the geodesic polyline between the two coordinates, and two markers: 如果两者都成功,则在两个坐标和两个标记之间绘制测地线折线:

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
  <title>Google Maps API Geocoding Demo</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
</head> 
<body>
  <div id="map" style="width: 500px; height: 400px;"></div>

  <script type="text/javascript">
    var address1 = 'London, UK';
    var address2 = 'New York, US';

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 2,
      center: new google.maps.LatLng(35.00, -25.00),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

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

    gc.geocode({'address': address1}, function (res1, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        gc.geocode({'address': address2}, function (res2, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            new google.maps.Marker({
              position: res1[0].geometry.location,
              map: map
            });

            new google.maps.Marker({
              position: res2[0].geometry.location,
              map: map
            });

            new google.maps.Polyline({
              path: [
                res1[0].geometry.location,
                res2[0].geometry.location
              ],
              strokeColor: '#FF0000',
              geodesic: true,
              map: map
            });
          }
        });
      }            
    });

  </script>
</body>
</html>

Screenshot: 截图:

Google Maps API地理编码演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM