如何通过给出lat lng获得省名?

时间:2010-09-21 09:27:45

标签: google-maps

我得到某个地方的lat& lng信息,我想知道这个地方所在的城市或省名,我该怎么做?

例如,在中国北京的这个lat& lng:Point(39.904667 116.408198),我希望得到北京。

谷歌地图有没有api可以做到的?

2 个答案:

答案 0 :(得分:7)

您所描述的内容称为Reverse Geocoding。 Google为此提供了两个API:A JavaScript APIWeb Service API

使用网络服务的示例:

  

http://maps.google.com/maps/api/geocode/json?latlng=39.904667,116.408198&sensor=false

响应(使用JSON,但也可以返回XML):

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "2号 Zhengyi Rd, Dongcheng, Beijing, China",
    "address_components": [ {
      "long_name": "2号",
      "short_name": "2号",
      "types": [ "street_number" ]
    }, {
      "long_name": "Zhengyi Rd",
      "short_name": "Zhengyi Rd",
      "types": [ "route" ]
    }, {
      "long_name": "Dongcheng",
      "short_name": "Dongcheng",
      "types": [ "sublocality", "political" ]
    }, {
      "long_name": "Beijing",
      "short_name": "Beijing",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Beijing",
      "short_name": "Beijing",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "China",
      "short_name": "CN",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 39.9042110,
        "lng": 116.4074130
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 39.9010634,
          "lng": 116.4042654
        },
        "northeast": {
          "lat": 39.9073586,
          "lng": 116.4105606
        }
      }
    }
  }, {
    "types": [ "sublocality", "political" ],
    "formatted_address": "Dongcheng, Beijing, China",
    "address_components": [ {
      "long_name": "Dongcheng",
      "short_name": "Dongcheng",
      "types": [ "sublocality", "political" ]
    }, {
      "long_name": "Beijing",
      "short_name": "Beijing",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Beijing",
      "short_name": "Beijing",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "China",
      "short_name": "CN",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 39.9284190,
        "lng": 116.4161900
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 39.9000758,
          "lng": 116.3868896
        },
        "northeast": {
          "lat": 39.9754256,
          "lng": 116.4471843
        }
      },
      "bounds": {
        "southwest": {
          "lat": 39.9000758,
          "lng": 116.3868896
        },
        "northeast": {
          "lat": 39.9754256,
          "lng": 116.4471843
        }
      }
    }
  }, {
    "types": [ "administrative_area_level_1", "political" ],
    "formatted_address": "Beijing, China",
    "address_components": [ {
      "long_name": "Beijing",
      "short_name": "Beijing",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "China",
      "short_name": "CN",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 39.9046670,
        "lng": 116.4081980
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 39.4432527,
          "lng": 115.4203742
        },
        "northeast": {
          "lat": 41.0608685,
          "lng": 117.5055691
        }
      },
      "bounds": {
        "southwest": {
          "lat": 39.4432527,
          "lng": 115.4203742
        },
        "northeast": {
          "lat": 41.0608685,
          "lng": 117.5055691
        }
      }
    }
  }, {
    "types": [ "locality", "political" ],
    "formatted_address": "Beijing, China",
    "address_components": [ {
      "long_name": "Beijing",
      "short_name": "Beijing",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Beijing",
      "short_name": "Beijing",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "China",
      "short_name": "CN",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 39.9046670,
        "lng": 116.4081980
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 39.6612714,
          "lng": 116.0119343
        },
        "northeast": {
          "lat": 40.2164962,
          "lng": 116.7829835
        }
      },
      "bounds": {
        "southwest": {
          "lat": 39.6612714,
          "lng": 116.0119343
        },
        "northeast": {
          "lat": 40.2164962,
          "lng": 116.7829835
        }
      }
    }
  }, {
    "types": [ "country", "political" ],
    "formatted_address": "China",
    "address_components": [ {
      "long_name": "China",
      "short_name": "CN",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 35.8616600,
        "lng": 104.1953970
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 17.9996000,
          "lng": 73.4994137
        },
        "northeast": {
          "lat": 53.5609740,
          "lng": 134.7728100
        }
      },
      "bounds": {
        "southwest": {
          "lat": 17.9996000,
          "lng": 73.4994137
        },
        "northeast": {
          "lat": 53.5609740,
          "lng": 134.7728100
        }
      }
    }
  } ]
}

这是使用JavaScript API的示例:

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

   <script type="text/javascript"> 

   var map = new google.maps.Map(document.getElementById('map'), { 
       mapTypeId: google.maps.MapTypeId.ROADMAP,
       zoom: 12
   });

   var infowindow = new google.maps.InfoWindow();

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

   geocoder.geocode({
      'latLng': new google.maps.LatLng(39.904667, 116.408198)
   }, 
   function(results, status) {
      if(status == google.maps.GeocoderStatus.OK) {
         var marker = new google.maps.Marker({
            position: results[0].geometry.location,
            map: map
         });
         map.setCenter(results[0].geometry.location);
         infowindow.setContent(results[1].formatted_address);
         infowindow.open(map, marker);
      }
   });

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

截图:

Reverse Geocoding

答案 1 :(得分:0)

不确定Google地图,但地理位置(http://www.geonames.org/)可能符合您的需求,尤其是这一个:http://www.geonames.org/export/web-services.html#countrysubdiv