2014-10-08 96 views
0

下面是使用Lat/long自動完成的代碼。 我希望結果是針對某個城市的。 我的意思是,搜索自動完成應該限制在一個城市。 幫助我..儘快如何限制自動完成結果(無地圖)到一個城市

<!DOCTYPE html> 
    <html> 
     <head> 
     <link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"> 
     <script src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places" type="text/javascript"></script> 

<script type="text/javascript"> 
$("#autocomplete").on('focus', function() { 
    geolocate(); 
}); 

var placeSearch, autocomplete; 
var componentForm = { 
    street_number: 'short_name', 
    route: 'long_name', 
    locality: 'long_name', 
    administrative_area_level_1: 'short_name', 
    country: 'long_name', 
    postal_code: 'short_name' 
}; 

function initialize() { 
    autocomplete = new google.maps.places.Autocomplete(
    /** @type {HTMLInputElement} */ (document.getElementById('autocomplete')), { 
     types: ['geocode'] 
    }); 

    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
     fillInAddress(); 
    }); 
} 

function fillInAddress() { 

    var place = autocomplete.getPlace(); 

    document.getElementById("latitude").value = place.geometry.location.lat(); 
    document.getElementById("longitude").value = place.geometry.location.lng(); 

    for (var component in componentForm) { 
     document.getElementById(component).value = ''; 
     document.getElementById(component).disabled = false; 
    } 

    for (var i = 0; i < place.address_components.length; i++) { 
     var addressType = place.address_components[i].types[0]; 
     if (componentForm[addressType]) { 
      var val = place.address_components[i][componentForm[addressType]]; 
      document.getElementById(addressType).value = val; 
     } 
    } 
} 

function geolocate() { 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function (position) { 
      var geolocation = new google.maps.LatLng(
      position.coords.latitude, position.coords.longitude); 

      var latitude = position.coords.latitude; 
      var longitude = position.coords.longitude; 
      document.getElementById("latitude").value = latitude; 
      document.getElementById("longitude").value = longitude; 

      autocomplete.setBounds(new google.maps.LatLngBounds(geolocation, geolocation)); 
     }); 
    } 

} 

initialize(); 

</script> 
     </head> 
<body onload="initialize()"> 
<div id="locationField"> 
    <input id="autocomplete" size="60" placeholder="Enter your address" type="text"></input> 
</div> 
<table id="address"> 
    <tr> 
     <td class="label">Lat</td> 
     <td class="slimField"> 
      <input type="text" class="field" id="latitude"></input> 
     </td> 
     <td class="label">Long</td> 
     <td class="wideField"> 
      <input type="text" class="field" id="longitude"></input> 
     </td> 
    </tr> 
</table> 
</body> 
</html> 
+0

你是什麼意思'限於一個城市'? – 2014-10-08 07:15:47

+0

在自動填充地址字段中,我希望爲特定城市完成的搜索只能說印度的班加羅爾。如果我正在輸入「Korm ....」,它會首先建議我在班加羅爾的「Kormangala」只有 – 2014-10-08 07:46:46

+0

你需要在這裏檢查http://stackoverflow.com/questions/8282026/how-to-limit-google-autocomplete-結果到城市和國家只 – 2014-10-08 07:53:34

回答

0
$.get("http://ipinfo.io", function (response) { 
    $("#ip").html("IP: " + response.ip); 
    $("#address").html("Location: " + response.city + ", " + response.region); 
    $("#details").html(JSON.stringify(response, null, 4)); 
}, "jsonp"); 

這裏是小提琴http://jsfiddle.net/zK5FN/2/會告訴你根據經緯度和長期的城市。
它使用ipinfo.io

+0

感謝您的答案,但我需要的是讓我解釋一個例子, 在自動填充地址字段我想要爲特定的城市完成搜索只能說印度班加羅爾。如果我輸入「Korm ....」,它只能在班加羅爾建議我「Kormangala」 – 2014-10-08 07:35:44

相關問題