Google地理编码状态始终为空 [英] Google geocode status always empty

查看:150
本文介绍了Google地理编码状态始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是地理编码的新手.我从用户那里找到邮政编码,找到纬度和经度,并将其与JSON中存在纬度和经度的一组附近位置进行比较.因此,我必须遍历每个事件进行比较.地理编码状态始终为空,因此我无法获得用户输入的邮政编码的经纬度.

I am new to geocode. I am getting the zip code from the user finding the lat and long and comparing it with a set of nearby locations who latitude and longitude are present in a JSON. So I have to loop through each event to compare. The geocode status is always empty and hence I am not able to get the lat and long for the zipcode which the user has entered.

<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循环.

After the line geocoder.geocode( { 'address': zip }, function(results, status) it skips directly to the for loop.

推荐答案

geocoder.geocode 异步运行,因此您需要等到其响应从Google的服务器传递出来后,才能使用已响应数据.将循环放入回调中:

geocoder.geocode works asyncronously, so you need to wait untill its response will be delivered from google's servers,and only then use reponded data.Put your loop inside of callback:

  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地理编码状态始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆