2017-10-28 7 views
1

現在のところ、結果をアイルランドとイギリス(GB)に限定することはできますが、結果をアイルランドの島(アイルランド&北アイルランド)北アイルランドは英国の一部です英国全体の国コードは& gbGoogle Maps APIのオートコンプリートの結果をアイルランドと北アイルランドに限定することはできますか

ですが、これを国・アイルランドと北北アイルランドにどのように限定しますか?事前にあなたの助けのための

おかげ

するvar countryCodes = [ 'すなわち'、 'ギガバイトの'];

 function initMap() { 
      var autoInputs = []; 

      // Destroy all existing inputs and reload them. 
      function autoAllInputs() { 
       autoInputs = []; 
       $.each($(".js-location-input"), function(i, input) { 
        var ainput = new google.maps.places.Autocomplete(input); 
        ainput.setComponentRestrictions({'country': countryCodes}); 
        autoInputs.push(ainput); 
       }); 
      } 

      var $template = $(".js-repeat-template"); 
      var $container = $(".js-repeated-items"); 

      $(".js-add-repeatable").on("click", function(e) { 
       e.preventDefault(); 
       $container.append($template.html()); 
       autoAllInputs(); 
      }); 

      autoAllInputs(); 
+0

現在のAPI呼び出しを含むように質問を更新してください –

+0

コードを追加しました。ありがとう –

答えて

0

オートコンプリートの場所に管理領域フィルタを追加する方法はありません。あなたのユースケースの唯一の回避策は、アイルランドと北アイルランドを含む境界を定義し、strictBoundsパラメータがautocomplete optionsでtrueに等しい場合にこれらの境界を設定することです。コンセプトの

証明

function initMap() { 
 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
    center: {lat: 53.435719, lng: -7.77832}, 
 
    zoom: 6, 
 
    scaleControl: true 
 
    }); 
 
    
 
    var irelandBounds = new google.maps.LatLngBounds(
 
    new google.maps.LatLng(50.999929,-10.854492), 
 
    new google.maps.LatLng(55.354135,-5.339355)); 
 
    
 
    map.fitBounds(irelandBounds); 
 
    
 
    var boundsPoly = new google.maps.Polyline({ 
 
    map: map, 
 
    path: [ 
 
     {lat: irelandBounds.getSouthWest().lat(), lng: irelandBounds.getSouthWest().lng()}, 
 
     {lat: irelandBounds.getSouthWest().lat(), lng: irelandBounds.getNorthEast().lng()}, 
 
     {lat: irelandBounds.getNorthEast().lat(), lng: irelandBounds.getNorthEast().lng()}, 
 
     {lat: irelandBounds.getNorthEast().lat(), lng: irelandBounds.getSouthWest().lng()}, 
 
     {lat: irelandBounds.getSouthWest().lat(), lng: irelandBounds.getSouthWest().lng()} 
 
    ], 
 
    strokeColor: "#FF0000" 
 
    }); 
 

 
    
 
    var input = /** @type {!HTMLInputElement} */(
 
     document.getElementById('pac-input')); 
 

 
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); 
 
    
 
    var autocomplete = new google.maps.places.Autocomplete(input, { 
 
    bounds: irelandBounds, 
 
    strictBounds: true 
 
    }); 
 

 
    var infowindow = new google.maps.InfoWindow(); 
 
    var marker = new google.maps.Marker({ 
 
    map: map, 
 
    anchorPoint: new google.maps.Point(0, -29) 
 
    }); 
 

 
    autocomplete.addListener('place_changed', function() { 
 
    infowindow.close(); 
 
    marker.setVisible(false); 
 
    var place = autocomplete.getPlace(); 
 
    if (!place.geometry) { 
 
     window.alert("Autocomplete's returned place contains no geometry"); 
 
     return; 
 
    } 
 

 
    // If the place has a geometry, then present it on a map. 
 
    if (place.geometry.viewport) { 
 
     map.fitBounds(place.geometry.viewport); 
 
    } else { 
 
     map.setCenter(place.geometry.location); 
 
     map.setZoom(17); // Why 17? Because it looks good. 
 
    } 
 
    marker.setIcon(/** @type {google.maps.Icon} */({ 
 
     url: place.icon, 
 
     size: new google.maps.Size(71, 71), 
 
     origin: new google.maps.Point(0, 0), 
 
     anchor: new google.maps.Point(17, 34), 
 
     scaledSize: new google.maps.Size(35, 35) 
 
    })); 
 
    marker.setPosition(place.geometry.location); 
 
    marker.setVisible(true); 
 

 
    var address = ''; 
 
    if (place.address_components) { 
 
     address = [ 
 
     (place.address_components[0] && place.address_components[0].short_name || ''), 
 
     (place.address_components[1] && place.address_components[1].short_name || ''), 
 
     (place.address_components[2] && place.address_components[2].short_name || '') 
 
     ].join(' '); 
 
    } 
 

 
    infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address); 
 
    infowindow.open(map, marker); 
 
    }); 
 
}
 html, body { 
 
     height: 100%; 
 
     margin: 0; 
 
     padding: 0; 
 
     } 
 
     #map { 
 
     height: 100%; 
 
     } 
 
.controls { 
 
    margin-top: 10px; 
 
    border: 1px solid transparent; 
 
    border-radius: 2px 0 0 2px; 
 
    box-sizing: border-box; 
 
    -moz-box-sizing: border-box; 
 
    height: 32px; 
 
    outline: none; 
 
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3); 
 
} 
 

 
#pac-input { 
 
    background-color: #fff; 
 
    font-family: Roboto; 
 
    font-size: 15px; 
 
    font-weight: 300; 
 
    margin-left: 12px; 
 
    padding: 0 11px 0 13px; 
 
    text-overflow: ellipsis; 
 
    width: 300px; 
 
} 
 

 
#pac-input:focus { 
 
    border-color: #4d90fe; 
 
} 
 

 
.pac-container { 
 
    font-family: Roboto; 
 
}
<input id="pac-input" class="controls" type="text" 
 
     placeholder="Enter a location"> 
 
<div id="map"></div> 
 
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&libraries=places&callback=initMap" 
 
     async defer></script>

私はこれが役に立てば幸い!

+0

パーフェクトなxomena! –

0

12. Maps API Standard Pricing Plan 12.1無料クォータ。 GoogleマップのJavaScript - :サービスの特定の部分が

は私が国に制限することなく、一日あたり

25000ダウンロードを考える.. Maps APIを文書で説明した取引限度額まで無償であなたに提供されていますAPI - GoogleのスタティックマップAPI - Googleのストリートビュー画像のAPI日あたり

2500要求: - Googleマップ行き方API - Googleマップディスタンス行列API 4 - Googleマップ標高API - GoogleマップジオコーディングAPI - GoogleマップのジオロケーションAPI - Googleマップ道路API - 現在、より多くを必要とする場合は、GoogleマップのタイムゾーンのAPI

contact Sales ..

+0

私は正常に動作して自動完成していますが、北アイルランドは私が英国を追加するときに問題を引き起こしているので、結果に北アイルランドが含まれています - 英国の結果全体も取得します –

+0

イギリスと北アイルランド= GB;アイルランド= IE ...北アイルランド+アイルランドが必要です。ok – Akubik

+0

&bounds = X1、Y1 | X2、Y2(XY "northeast":{"lat":1.0000、 "lng":0.0000}、 "southwest" {"lat":0.0000、 "lng":1.0000})とxomenaからの回答(TYは注意のため) – Akubik

関連する問題