Google地图地理编码(地址为GLatLng) [英] Google Maps geocoding (address to GLatLng)

查看:128
本文介绍了Google地图地理编码(地址为GLatLng)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从两个地址点中使用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);






有人能告诉我如何动态地址解析为GLatLng坐标?阅读 Google API文档后,我有点困惑。

解决方案

您可能想查看下面的示例。首先它尝试对 address1 进行地理编码。如果成功,它会尝试对 address2 进行地理编码。如果两者都成功,则绘制两个坐标和两个标记之间的测地折线:

 <!DOCTYPE html> 
< html>
< head>
< meta http-equiv =content-typecontent =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 =mapstyle =width:500px; height:400px;>< / div>

< script type =text / javascript>
var address1 ='伦敦,英国';
var address2 ='纽约,美国';

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
});

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',
测地线:true,
地图:map
});
}
});
}
});

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

截图:


I am trying to draw a geodesic polyline with Google Maps JavaScript API from two address points.

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? I am a little confused after reading Google's API documentation.

解决方案

You may want to check out the following example. First it tries to geocode address1. If it succeeds, it tries to geocode 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地图地理编码(地址为GLatLng)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆