Javascript 谷歌地理编码状态始终为空
我不熟悉地理编码。我从查找lat和long的用户那里获取邮政编码,并将其与JSON中显示纬度和经度的一组附近位置进行比较。所以我必须循环每个事件以进行比较。地理编码状态始终为空,因此我无法获取用户输入的zipcode的lat和longJavascript 谷歌地理编码状态始终为空,javascript,google-maps-api-3,geocode,google-geocoder,Javascript,Google Maps Api 3,Geocode,Google Geocoder,我不熟悉地理编码。我从查找lat和long的用户那里获取邮政编码,并将其与JSON中显示纬度和经度的一组附近位置进行比较。所以我必须循环每个事件以进行比较。地理编码状态始终为空,因此我无法获取用户输入的zipcode的lat和long <script src="http://maps.google.com/maps/api/js?sensor=false"></script> 这是我的Js函数 self.find_events = function find_eve
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
这是我的Js函数
self.find_events = function find_events(zip, eventId) {
var data = LAH.events.events_data,
matches_found = 0;
var today = new Date();
var zip = zip || null;
var eventId = eventId || null;
geocoder = new google.maps.Geocoder();
if(geocoder){
geocoder.geocode( { 'address': zip }, function(results, status) { // status is empty
if (status == google.maps.GeocoderStatus.OK) {
var userLat = results[0].geometry.location.lat();
var userLng = results[0].geometry.location.lng();
userLatLng = results[0].geometry.location;
}
});//end geocode
}
for (var i = data.length-1; i--;) {
if (eventId === null) {
var eventEnd = data[i].endDate;
var calc_dis = calculateDistance(userLat, userLng, parseFloat(data[i].lat), parseFloat(data[i].lng));
if ((zip == 'all' || calc_dis === true) && today < eventEnd) {
display_event(data[i]);
matches_found += 1;
}
}
else {
// eventId is valid, only display what we found in the query string
if (data[i].eventId === parseInt(eventId, 10)) {
display_event(data[i]);
matches_found += 1;
}
}
}
matches_found ? display_table() : display_no_results();
return matches_found;
};
self.find_events=函数find_events(zip,eventId){
var数据=LAH.events.events\u数据,
找到的匹配项=0;
var today=新日期();
var zip=zip | | null;
var eventId=eventId | | null;
geocoder=新的google.maps.geocoder();
如果(地理编码器){
geocoder.geocode({'address':zip},函数(结果,状态){//状态为空
if(status==google.maps.GeocoderStatus.OK){
var userLat=results[0]。geometry.location.lat();
var userLng=results[0]。geometry.location.lng();
userLatLng=results[0]。geometry.location;
}
});//结束地理代码
}
对于(var i=data.length-1;i--;){
if(eventId==null){
var eventEnd=data[i].endDate;
var calc_dis=calculateInstance(userLat、userLng、parseFloat(数据[i].lat)、parseFloat(数据[i].lng));
如果((zip='all'| | calc|u dis===true)&&today
第geocoder.geocode({'address':zip},函数(结果,状态)之后它直接跳转到for循环
地理编码程序。地理编码
异步工作,因此您需要等待,直到它的响应从谷歌的服务器发送,然后才使用转发的数据。将您的循环放入回调中:
geocoder.geocode( { 'address': zip }, function(results, status) { // status is empty
if (status == google.maps.GeocoderStatus.OK) {
var userLat = results[0].geometry.location.lat();
var userLng = results[0].geometry.location.lng();
userLatLng = results[0].geometry.location;
for (var i = data.length-1; i--;) {
//loop body
}
}
});//end geocode
geocoder.geocode
是异步工作的,因此您需要等待,直到它的响应从google的服务器发送,然后才使用转发的数据。将您的循环放入回调中:
geocoder.geocode( { 'address': zip }, function(results, status) { // status is empty
if (status == google.maps.GeocoderStatus.OK) {
var userLat = results[0].geometry.location.lat();
var userLng = results[0].geometry.location.lng();
userLatLng = results[0].geometry.location;
for (var i = data.length-1; i--;) {
//loop body
}
}
});//end geocode
向google map api发出ajax请求的区别或优势是什么?address=ZIP&sensor=false并解析JSON并获取ZIP。您无法向google服务发出ajax请求,因为。因此,使用google javascript api的唯一方法(如
google.maps.Geocoder
,google.maps.DistanceService
,等等)内部使用的服务。向google map api发出ajax请求的区别或优势是什么?address=ZIP&sensor=false并解析JSON并获取ZIP。您不能向google服务发出ajax请求,因为。因此,使用google javascript api的唯一方法是(如google.maps.Geocoder
,google.maps.DistanceService
等)内部使用的服务。