2011-09-07 178 views
0

我有一個地址數據庫,我希望能夠讓用戶在地圖上放下一個精確定位點,並根據該點的駕駛距離定義一個區域。我想知道是否有方法通過該用戶定義的區域來搜索我的數據庫。谷歌地圖按自定義區域搜索地址

回答

0

你應該能夠得到針落地的經度和緯度座標,然後將數據發送到服務器,並使用此頁上列出的查詢返回結果:http://code.google.com/apis/maps/articles/phpsqlsearch.html

SELECT id, (3959 * acos(cos(radians(37)) * cos(radians(lat)) * cos(radians(lng) - radians(-122)) + sin(radians(37)) * sin(radians(lat)))) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

你將需要將地址編碼爲經度和緯度,然後將它們與每個地址一起存儲。這是我用來刮經度和緯度地區,以我的數據庫地址確切的代碼:

/* Geocode Address 
    /***********************/ 
    $query = $db->query("SELECT * FROM wholesale_clients WHERE hq_id IN (SELECT owner_id FROM storelocator)"); 
    while($info = $query->fetch_array()) { 
    $address = str_replace(' ', '+', $info['address_1'] . ' ' . $info['address_2'] . ' ' . $info['suburb'] . ' ' . $info['state'] . ' ' . $info['zip'] . ' Australia'); 
    $file = file_get_contents('https://maps.googleapis.com/maps/api/geocode/xml?address='.$address.'&sensor=false'); 
    $file = xml2array($file); 
    $lat = $file['GeocodeResponse']['result']['geometry']['location']['lat']; 
    $lng = $file['GeocodeResponse']['result']['geometry']['location']['lng']; 
    //echo $info['hq_id'] . ' ' . $lat . ' ' . $lng . '<br />'; 
    if(!empty($lat) && !empty($lng)) { 
    $db->query("UPDATE `storelocator` SET `lat` = '" . $lat . "', `lng` = '" . $lng . "', `updated` = 1 WHERE `owner_id` = '" . $info['hq_id'] . "'"); 
    } 
    } 

    /***********************/ 

XML2array功能:http://www.bin-co.com/php/scripts/xml2array/

+0

所以我需要什麼並不像真正駕駛距離的烏鴉那樣蒼蠅。然而,我可以做的是在我得到這個清單後,我可以使用谷歌駕車距離再次過濾:http://briancray.com/2009/06/23/calculate-driving-distance-google-maps-api/ – JAstanton