ジオコーディングは初めてです。緯度と経度を見つけて、JSONに緯度と経度が存在する近くの場所のセットと比較しているユーザーから郵便番号を取得しています。したがって、比較するために各イベントをループする必要があります。地理コードのステータスは常に空であるため、ユーザーが入力した郵便番号の緯度と経度を取得できません。
<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;
};
行geocoder.geocode({'address':zip}、function(results、status)の後、直接forループにスキップします。