30

GoogleのGeocoderを使用して、特定の住所の緯度座標を検索しています。

    var geocoder = new google.maps.Geocoder();
    geocoder.geocode(
    {
        'address':  address,
        'region':   'uk'
    }, function(results, status) {
        if(status == google.maps.GeocoderStatus.OK) {
            lat: results[0].geometry.location.lat(),
            lng: results[0].geometry.location.lng()
    });

address変数は入力フィールドから取得されます。

英国だけで場所を検索したい。'region': 'uk'指定するだけで十分だと思いましたが、そうではありません。「ボストン」と入力すると、米国でボストンが見つかり、英国でボストンが欲しかったのです。

ジオコーダーが1つの国からのみ、または特定の緯度範囲からの場所を返すように制限するにはどうすればよいですか?

ありがとう

4

13 に答える 13

27

次のコードは、住所を変更することなく、英国で最初に一致する住所を取得します。

  var geocoder = new google.maps.Geocoder();
  geocoder.geocode(
  {
    'address':  address,
    'region':   'uk'
  }, function(results, status) {
    if(status == google.maps.GeocoderStatus.OK) {
        for (var i=0; i<results.length; i++) {
            for (var j=0; j<results[i].address_components.length; j++) {
               if ($.inArray("country", results[i].address_components[j].types) >= 0) {
                    if (results[i].address_components[j].short_name == "GB") {
                        return_address = results[i].formatted_address;
                        return_lat = results[i].geometry.location.lat();
                        return_lng = results[i].geometry.location.lng();
                        ...
                        return;
                    }
                }
            }
        }
    });
于 2012-02-01T14:16:31.757 に答える
27

componentRestrictions属性を使用します。

geocoder.geocode({'address': request.term, componentRestrictions: {country: 'GB'}}
于 2013-11-08T10:25:44.993 に答える
22

更新:この答えはもはや最善のアプローチではないかもしれません。詳細については、回答の下のコメントを参照してください。


Pekkaがすでに提案したことに加えて、次の例のように、に連結', UK'することをお勧めします。address

<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Google Maps Geocoding only in UK Demo</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 mapOptions = { 
      mapTypeId: google.maps.MapTypeId.TERRAIN,
      center: new google.maps.LatLng(54.00, -3.00),
      zoom: 5
   };

   var map = new google.maps.Map(document.getElementById("map"), mapOptions);
   var geocoder = new google.maps.Geocoder();

   var address = 'Boston';

   geocoder.geocode({
      'address': address + ', UK'
   }, 
   function(results, status) {
      if(status == google.maps.GeocoderStatus.OK) {
         new google.maps.Marker({
            position:results[0].geometry.location,
            map: map
         });
      }
   });

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

スクリーンショット:

英国でのみジオコーディング

これは非常に信頼できると思います。一方、次の例は、この場合region、パラメーターもパラメーターも効果がないことを示しています。bounds

<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Google Maps Geocoding only in UK Demo with Bounds</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: 300px"></div> 

   <script type="text/javascript"> 

   var mapOptions = { 
      mapTypeId: google.maps.MapTypeId.TERRAIN,
      center: new google.maps.LatLng(50.00, -33.00),
      zoom: 3
   };

   var map = new google.maps.Map(document.getElementById("map"), mapOptions);   
   var geocoder = new google.maps.Geocoder();

   // Define north-east and south-west points of UK
   var ne = new google.maps.LatLng(60.00, 3.00);
   var sw = new google.maps.LatLng(49.00, -13.00);

   // Define bounding box for drawing
   var boundingBoxPoints = [
      ne, new google.maps.LatLng(ne.lat(), sw.lng()),
      sw, new google.maps.LatLng(sw.lat(), ne.lng()), ne
   ];

   // Draw bounding box on map    
   new google.maps.Polyline({
      path: boundingBoxPoints,
      strokeColor: '#FF0000',
      strokeOpacity: 1.0,
      strokeWeight: 2,
      map: map
   });

   // Geocode and place marker on map
   geocoder.geocode({
      'address': 'Boston',
      'region':  'uk',
      'bounds':  new google.maps.LatLngBounds(sw, ne)
   }, 
   function(results, status) {
      if(status == google.maps.GeocoderStatus.OK) {
         new google.maps.Marker({
            position:results[0].geometry.location,
            map: map
         });
      }
   });

   </script> 
</body> 
</html>
于 2010-04-15T16:31:59.813 に答える
16

これを行う正しい方法は、componentRestrictionsを提供することです。

例えば:

var request = {
    address: address,
    componentRestrictions: {
        country: 'UK'
    }
}
geocoder.geocode(request, function(results, status){
    //...
});
于 2014-02-27T22:26:14.470 に答える
8

ドキュメントによると、regionパラメータは(その領域への実際の制限ではなく)バイアスのみを設定しているようです。APIが英国の場所で正確な住所を見つけられない場合、入力した地域に関係なく検索を拡張すると思います。

私は過去に、 (地域に加えて)住所に国コードを指定することでかなりうまくいきました。しかし、私はまだ異なる国で同じ地名を使った経験はあまりありません。それでも、それは一撃の価値があります。試す

'address': '78 Austin Street, Boston, UK'

(米国ボストンの代わりに)アドレスを返さないようにする必要があります。

'address': '78 Main Street, Boston, UK'

実際にはメインストリートがあるので、英国のボストンに戻る必要があります。

アップデート:

ジオコーダーが1つの国からのみ、または特定の緯度範囲からの場所を返すように制限するにはどうすればよいですか?

パラメータを設定できboundsます。こちらをご覧ください

もちろん、そのためには英国サイズの長方形を計算する必要があります。

于 2010-04-15T16:26:54.630 に答える
5

「、UK」の配置、リージョンのUKへの設定、および境界の設定に問題が見つかりました。ただし、3つすべてを実行すると、問題が解決するようです。スニペットは次のとおりです:-

var sw = new google.maps.LatLng(50.064192, -9.711914)
var ne = new google.maps.LatLng(61.015725, 3.691406)
var viewport = new google.maps.LatLngBounds(sw, ne);

geocoder.geocode({ 'address': postcode + ', UK', 'region': 'UK', "bounds": viewport }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
.....etc.....
于 2012-01-05T15:02:31.910 に答える
2

私は次のことを試しました:

geocoder.geocode( {'address':request.term + ', USA'}

そして、それは特定の地域(米国の国)のために私のために働いています。

于 2012-02-27T04:39:29.693 に答える
1

結果は変化する可能性があり、地域が機能していないように見えるため、これはフィルタリングが非常に簡単であることが常にわかりました。

response( $.map( results, function( item ) {
 if (item.formatted_address.indexOf("GB") != -1) {
    return {
      latitude: item.geometry.location.lat(),
      longitude: item.geometry.location.lng()
    }
  }
}
于 2013-04-23T07:18:54.373 に答える
0

英国の場合、地域にはGBを使用する必要があります。英国はISO国コードではありません!

于 2011-03-24T19:44:03.797 に答える
0

私はハイブリッドアプローチを好みます。

  1. componentRestrictionsを使用して、最初に国を厳しく制限します。
  2. それでも十分な結果が得られない場合は、より広範囲に検索します(必要に応じてバイアスを再導入します)

    function MyGeocoder(address,region)
    {
        geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'address': address, 'componentRestrictions': { 'country': region } }, function (r, s) {
            if (r.length < 10) geocoder.geocode({ 'address': address /* could also bias here */ }, function (r2, s2) {
                for (var j = 0; j < r2.length; j++) r.push(r2[j]);
                DoSomethingWithResults(r);
            });
            else DoSomethingWithResults(r);
        });
    }
    function DoSomethingWithResults(r) { // Remove Duplicates var d = {}; r = r.filter(function (e) { var h = e.formatted_address.valueOf(); var isDup = d[h]; d[h] = true; return !isDup; });

    // Do something with results }

于 2014-04-24T17:04:05.963 に答える
0

多くのあいまいなクエリでは、どこを見ればよいかを伝えようとしても、Googleでは常に米国が優先されます。応答を見て、出力国= USの場合はおそらく無視できますか?

それが、私がしばらく前にGoogle Geocoderの使用をやめ、2年前に自分で作成し始めた主な理由です。

https://geocode.xyz/Boston,%20UKは、常に英国の場所を返します。region = UKを追加することで、さらに確実にすることができます:https ://geocode.xyz/Boston,%20UK?region = UK

于 2018-04-28T12:10:17.533 に答える
0

英国全土にボーダーを作成し、LatとLngが範囲内にあるかどうかを確認しました。3 kのアドレスの場合、米国には約10から20のアドレスがあります。私はそれらを無視します(私の場合はそれを行うことができます)自動ズームを使用して静的マップ上にマルチマーカーを作成するためにlatとlngを使用します。私は私の解決策を共有します多分これは誰かの助けになるでしょう。また、私の場合のさまざまな解決策を聞いてうれしいです。

    private static string ReturnLatandLng(string GeocodeApiKey, string address)
    {
        string latlng = "";

        Geocoder geocoder = new Geocoder(GeocodeApiKey);

        var locations = geocoder.Geocode(address);

        foreach (var item in locations)
        {

            double longitude = item.LatLng.Longitude;
            double latitude = item.LatLng.Latitude;
            double borderSouthLatitude = 49.895878;
            double borderNorthLatitude = 62.000000;
            double borderWestLongitude = -8.207676;
            double borderEastLongitude = 2.000000;

            //Check If Geocoded Address is inside of the UK
            if (( (borderWestLongitude < longitude) && (longitude < borderEastLongitude) ) && ( (borderSouthLatitude < latitude) && (latitude < borderNorthLatitude) ) )
            {
                latlng = item.LatLng.ToString();
            }
            else
            {
                latlng = "";
                Console.WriteLine("GEOCODED ADDRESS IS NOT LOCATED IN UK ADDRESSES LIST. DELETING MARKER FROM MAP.....");
            }
        }
        return latlng;
    }
于 2019-03-15T11:06:19.327 に答える
-1

今日は自分で結果を国にフィルターする必要がありました。componentRestrictions:country:の2文字の国コードが機能しないことがわかりました。しかし、完全な国名はそうです。

これは、結果のaddress_componentsのフルネームです。

于 2015-04-20T10:44:10.770 に答える