0

我在我的网站上创建了一个表单,允许用户在表单中输入邮政编码,然后找到与我存储在数据库中的位置的邮政编码最接近的匹配项,并显示这两个位置之间的距离

当输入有效的邮政编码时,这可以正常工作,但有时当输入无效的邮政编码或世界其他地方有类似的位置时,它会返回一个奇怪的地址。例如,我的数据库中的一个邮政编码是 cr4,但是当他的值“cr”被放入我的查找器时,它将返回位置“加利福尼亚”作为不正确的起点。

我的问题是,是否允许 Google Maps API 仅搜索邮政编码位置?我在文档中搜索过,似乎在任何地方都看不到这个。

这是我的代码:

我的数据库中某个位置的示例:

 {
    address1: "Unit 2",
    address2: "Leslie Retail Park",
    address3: "Boucher Road",
    address4: "Belfast",
    postcode: "BT12 6HH",
    name: "Belfast",
    lat: 54.577708,
    lon: -5.961649,
    opening_hours: "Mon-Thu: 6:30 AM - 10:00 PM ^Fri: 6:30 AM - 8:00 PM ^Sat-Sun: 9:00 AM - 5:00 PM ^Bank Holidays: 9:00 AM - 5:00 PM",
    bank_holidays: "",
    id: 1,
    targetpage: "<?php echo $this->baseurl; ?>/index.php/our-gyms/fitspace-belfast",
    image: "<?php echo $this->baseurl; ?>/images/belfast.jpg"
  }

和我的 Google Maps API 搜索代码:

var geocoder, location1, location2;

    jQuery('#postcode_search_form').submit(function(e) {    
        e.preventDefault();
        geocoder = new GClientGeocoder();
        geocoder.getLocations(document.forms[0].postcode_search.value, function (response) {    
           if (response || response.Status.code != 200) {
                location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
                calculateDistance();
            }
        });
    });

    function calculateDistance()
    {
        var closeClubs = [];
        var sortable = [];
        var resultsArray = new Array();
        try {   
            jQuery(gyms).each(function(i, v) {
                var glatlng1 = new GLatLng(location1.lat, location1.lon);
                var glatlng2 = new GLatLng(v.lat, v.lon);
                var miledistance = glatlng1.distanceFrom(glatlng2, 3959).toFixed(1);
                if(miledistance <= 50) {
                    gyms[i].distance = miledistance;
                    closeClubs.push(v);
                }

                var kmdistance = (miledistance * 1.609344).toFixed(1);  
            });
            closeClubs.sort(function(a, b) {
                return a.distance - b.distance;
            });
            document.getElementById('club_search_start').style.display = "block";
            document.getElementById('club_search_start').innerHTML = '<strong>Your location: </strong>' + location1.address;

            var html = "";
            for(var i = 0; i < closeClubs.length; i++) {
                html += "<div class='club'>";
                    html += "<div class='result_left'><img src='" + closeClubs[i].image + "' /></div>";
                    html += "<div class='result_right'><a href='" + closeClubs[i].targetpage + "'>" + closeClubs[i].name + "</a>"; 
                    html += "<p class='club_postcode'>" + closeClubs[i].postcode + "</p>";
                    html += "<p class='club_description'>" + closeClubs[i].opening_hours + "</p>";  
                    html += "<p class='distance'>Distance:" + closeClubs[i].distance + " miles </p>";
                    html += "</div>"
                html += "</div>"
            }
            jQuery('#club_search_results').hide().html(html).fadeIn('slow');
        }
        catch (error) {
            alert(error);
        }
    }   
});

谢谢!

4

0 回答 0