呼び出し方法は下記のようになる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | var geocoder = new GClientGeocoder(); var center = map.getCenter(); geocoder.getLocations( center, function (response) { if (!response || response.Status.code != 200) { alert( "データが取得できませんでした" ); } else { var place = response.Placemark[0]; var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]); // Code here... var address = place.AddressDetails.Country.AddressLine[0]; // Retrieve address for whatever you want to... } }); |
getLocationsのオーバーロードとして、文字列の住所を受け取るものと、GLatLngを受け取るものと両方あるので用途に分けて使用して欲しい。
getLocationsで返却されるresponseの中身であるJSONクラスは下記になる(このページより抜粋)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | { "name" : "1600 Amphitheatre Parkway, Mountain View, CA, USA" , "Status" : { "code" : 200, "request" : "geocode" }, "Placemark" : [ { "address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA" , "AddressDetails" : { "Country" : { "CountryNameCode" : "US" , "AdministrativeArea" : { "AdministrativeAreaName" : "CA" , "SubAdministrativeArea" : { "SubAdministrativeAreaName" : "Santa Clara" , "Locality" : { "LocalityName" : "Mountain View" , "Thoroughfare" : { "ThoroughfareName" : "1600 Amphitheatre Pkwy" }, "PostalCode" : { "PostalCodeNumber" : "94043" } } } } }, "Accuracy" : 8 }, "Point" : { "coordinates" : [-122.083739, 37.423021, 0] } } ] } |
GClientGeocoderが独自のCacheメカニズムを実装しているので同じアドレスであればServerへのRound tripは発生しない。しかし、複数のアドレスを問い合わせるためにGClientGeocoderをループ内で複数回使用することは推奨されていないので、その場合はサーバ側などでHTTP Geocoderを使用するのがよいだろう。
0 件のコメント:
コメントを投稿