0

我有一个地址数据库,我希望能够让用户在地图上放置一个精确点,并根据该点周围的行驶距离定义一个区域。我想知道是否有办法按该用户定义的区域搜索我的数据库。

4

2 回答 2

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/

于 2011-09-07T14:15:10.210 回答
0

对于使用Kilometers的国际系统,将脚本的一部分替换为以下脚本:

6371 * acos( cos( radians(LATITUDE1) ) * cos( radians( LATITUDE2 ) ) * cos( radians( LONGITUDE1 ) - radians(LONGITUDE2) ) + sin( radians(LATITUDE1) ) * sin( radians( lat ) ) ) AS DISTANCE

资料来源: http: //www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe#MySQL

于 2012-02-06T01:37:21.477 回答