如何从Google地图地理编码返回地址类型? [英] How to return address types from google maps geocode?
问题描述
我目前使用Google地图api来在地图上对地理位置进行地理编码,并返回街道地址。
我目前使用以下代码返回地址:
pre $ 函数codeLatLng(markerPos){
geocoder.geocode({'latLng':markerPos},函数(结果,状态){
if(status == google.maps.GeocoderStatus.OK){
if(results [1]){
//设置marker地址变量
var markerAddress =结果[0] .formatted_address;
alert(markerAddress);
...
但是如果我不想返回格式化地址,而是使用地址组件类型更详细的版本,我该如何返回某些地址值,如: http://code.google.com/apis/maps/documentation/geocoding/#Types
帮助表示赞赏。
请问为什么要检查 results [1]
,然后使用结果[0]
(或者它只是一个我应该忽略的错字)?
只要 status
是 OK
,至少有一个结果是。
否则, status
会是 ZERO_RESULTS
。
<无论如何,你可以使用这样的东西:
function codeLatLng(markerPos){
geocoder.geocode({ 'latLng':markerPos},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
var markerAddress = results [0] .address_components [0] .long_name
+'(type:'+ results [0] .address_components [0] .types [0] +')';
alert(markerAddress);
你可以在整个 address_components
数组中玩很多,玩得开心!:)
如想获得更多乐趣,请在 http://gmaps-samples-v3.googlecode.com/svn/trunk/geocoder/v3-geocoder-tool。 HTML
I am currently using the google maps api to geocode locations on the map and return the address of the street.
I currently return the address with the following code:
function codeLatLng(markerPos) {
geocoder.geocode({'latLng': markerPos}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
//Set markerAddress variable
var markerAddress = results[0].formatted_address;
alert(markerAddress);
...
But what if I don't want to return the formatted address but a more detailed version using Address Component Types, how can I return certain address values like: http://code.google.com/apis/maps/documentation/geocoding/#Types
Help appreciated.
May I ask, why are you checking for results[1]
and then using results[0]
(or is it just a typo I should ignore)?
As long as status
is OK
, there will be at least one result.
Otherwise, status
would be ZERO_RESULTS
.
Anyway, you can use something like this:
function codeLatLng(markerPos) {
geocoder.geocode({'latLng': markerPos}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var markerAddress = results[0].address_components[0].long_name
+ ' (type: ' + results[0].address_components[0].types[0] + ')';
alert(markerAddress);
You can play a lot with the whole address_components
array, have fun! :)
For even more fun, have a look at the (source code of the) Google Maps API v3 Geocoder Tool at http://gmaps-samples-v3.googlecode.com/svn/trunk/geocoder/v3-geocoder-tool.html
这篇关于如何从Google地图地理编码返回地址类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!