【发布时间】:2013-05-06 04:12:00
【问题描述】:
我是 jquery 的新手,但我现在有点困难。据我所知,最终的想法是执行以下操作:
- 用户输入地址并点击搜索
- Google 地理编码器运行
- 如果没有结果则错误,或者获取坐标、州、邮编、国家、城市并在消息框中打印
我目前的代码甚至无法完成从输入框中抓取坐标的过程。它一直告诉我它需要一个支架,但无论我把支架放在哪里,我都无法让它运行。
这是我的尝试:
http://jsfiddle.net/QA7Xr/16/
完整代码如下:
var myLatlng = new google.maps.LatLng(31.272410, 0.190898);
// INITALIZATION
function initialize() {
var mapOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}
// GEOCODE
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
alert("Geocode was successful");
};
} else {
alert("Geocode was not successful for the following reason: " + status);
};
};
}
// GET ADDRESS DETAILS
function getLatLongDetail() {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'latLng': myLatlng
},
function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var address = "",
city = "",
state = "",
zip = "",
country = "";
for (var i = 0; i < results[0].address_components.length; i++) {
var addr = results[0].address_components[i];
// check if this entry in address_components has a type of country
if (addr.types[0] == 'country') country = addr.long_name;
else if (addr.types[0] == 'street_address') // address 1
address = address + addr.long_name;
else if (addr.types[0] == 'establishment') address = address + addr.long_name;
else if (addr.types[0] == 'route') // address 2
address = address + addr.long_name;
else if (addr.types[0] == 'postal_code') // Zip
zip = addr.short_name;
else if (addr.types[0] == ['administrative_area_level_1']) // State
state = addr.long_name;
else if (addr.types[0] == ['locality']) // City
city = addr.long_name;
}
alert('City: ' + city + '\n' + 'State: ' + state + '\n' + 'Zip: ' + zip + '\n' + 'Country: ' + country);
}
}
});
}
initialize();
getLatLongDetail();
谁能告诉我哪里出错了?我已经走到了尽头。
【问题讨论】:
-
您查看过控制台中记录的错误吗? Uncaught SyntaxError: Unexpected token { fiddle.jshell.net:44
-
纠正你的小提琴。忍受我。
标签: javascript jquery google-maps google-maps-api-3 geolocation