我希望在我的一个项目中使用 google place 自动完成功能,但问题是我需要将县、州和城市的条目存储为 mysql 中的不同条目。即当有人键入妈妈并且结果显示为孟买(城市)、马哈拉施特拉邦(州)、印度(国家)时,那么所有这 3 个条目都应该作为不同的条目存储在 mysql 中。是否有可能做到这一点 ?
问问题
469 次
2 回答
1
请参阅此页面并享受。
<!DOCTYPE HTML>
<html>
<head>
<title>Place Autocomplete Address and type</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false&language=it"></script>
</head>
<body>
<input id="autocomplete" placeholder="Enter your address"
type="text"></input>
<br>
<br>
<div id="divToPrint" style="clear: both; float: left; border:solid 1px black; height: 32px;"></div>
<script>
/* set a default Bounds */
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(52.207606672865225, -0.9448242187499911),
new google.maps.LatLng(52.464377026041284, -0.2746582031249841));
/* get a input tag with id = autocomplete */
var input = document.getElementById('autocomplete');
/* set a options */
var options = {
bounds: defaultBounds,
types: ['geocode'],
};
/* register a autocomplete object into a variable named autocomplete */
var autocomplete = new google.maps.places.Autocomplete(input, options);
/* listen a place_changed event into variable autocomplete and do: */
google.maps.event.addListener(autocomplete, 'place_changed', function() {
/* reset a defaultBounds into variable */
autocomplete.setBounds(defaultBounds);
/* get a response place */
var place = autocomplete.getPlace();
/* initilize the componentsTypePlace */
var componentsTypePlace = "";
/* get a div tag with id = divToPrint */
var divToPrint = document.getElementById('divToPrint');
/* get a legth of request address */
var addressLengthplace = place.address_components.length;
/* cycle into a adress_components */
for(var i=0; i < addressLengthplace; i++){
/* get a value of address_component type */
componentsTypePlace = place.address_components[i].types[0];
/* get a value of address_component array[i] */
var val = place.address_components[i].long_name;
/* insert into div a value of address component and its type */
divToPrint.innerHTML += "//"+ componentsTypePlace + " : " + val +" //";
}
});
</script>
</body>
</html>
于 2013-11-29T17:14:21.940 回答
0
<link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
<script>
var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'long_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();
});
}
函数填充地址(){
var place = autocomplete.getPlace();
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);
autocomplete.setBounds(new google.maps.LatLngBounds(geolocation,
geolocation));
});
}
}
</script>
<body onload="initialize()">
<div id="locationField">
<input id="autocomplete" placeholder="Enter your address" onFocus="geolocate()" type="text" value= <?php if(isset($location)): echo $location; else: echo '""'; endif; ?> name="data[Location][location]" class ="input-block-level" required ="required">
</div>
</body>
于 2014-01-28T04:52:06.513 回答