7

位置情報サービス アプリケーションに Google マップと Google ジオコーディング サービスを使用しています。住所を緯度/経度の位置に変換するために Google Geocoding サービスを使用しています。私の問題は、maps.google.com のように、特定の住所に適したズームを自動的に見つける方法です。

たとえば、maps.google.com で通りを検索すると (例: Cisitu Baru, Bandung)、通りが小さいズームで表示されます。地域 (例: ) を検索するBandungと、より大きなズームが表示されます。州の拡大ズーム (例: Jawa Barat/ West Java) など。

私は両方を試しました

var geocoder = new google.maps.Geocoder();
geocoder.geocode( {
    'address': someAddress
}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        console.dir(results);
        //cut
        map.panToBounds(results[0].geometry.bounds); //setting bound
        //cut
    }
});

//cut
map.panToBounds(results[0].geometry.viewports); //setting bound
//cut

(正直なところ、 code.google.com/apis/maps/documentation/javascript/geocoding.html からの と の使用boundsの違いはまだわかりません)viewport

ただし、どちらも適切なズームでマップを表示しません。

今、私はこのような小さなハックを使用しています

var tabZoom =  {
    street_address: 15,
    route: 15,
    sublocality: 14,
    locality: 13,
    country: 10
};
//cut
map.setCenter(results[0].geometry.location);
if (tabZoom[results[0].types[0]] != undefined){
    map.setZoom(tabZoom[results[0].types[0]]);
} else {
    map.zetZoom(10);
}
//cut

他の解決策はありますか?(または、まだ知らない Google Map API の何か?)

ありがとう!

4

2 に答える 2

4

GLatLngBoundsクラスを使用する

例:

// map: an instance of GMap2

// latlng: an array of instances of GLatLng

var latlngbounds = new GLatLngBounds( );

for ( var i = 0; i < latlng.length; i++ )
{
    latlngbounds.extend( latlng[ i ] );
}

map.setCenter( latlngbounds.getCenter( ), map.getBoundsZoomLevel( latlngbounds ) );

^

秘訣は、マップ上に同時に表示する必要があるすべてのポイントのリストをGLatLngBoundsオブジェクトに追加することです。Google Maps APIは、残りの計算を実行できます。

またはv3では、LatLngBoundsクラス(v2のGLatLngBoundsと同様)を使用できます。リンク:http ://code.google.com/apis/maps/documentation/javascript/reference.html#LatLngBounds

例については、これをチェックしてください:http: //unicornless.com/code/google-maps-v3-auto-zoom-and-auto-center

于 2012-01-09T08:40:41.540 に答える
3

結果ジオメトリのビューポートを使用します。検索結果に特定の境界がない場合、geometry.bounds でエラーが発生します。

ビューポートは、結果を最適に表示します。

map.fitBounds(results[0].geometry.viewport);

于 2013-03-10T17:49:53.223 に答える