Google 地图按自定义区域搜索地址

发布于 2024-12-03 12:29:49 字数 87 浏览 1 评论 0原文

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

I have a database of addresses and I want to be able to have a user drop a pinpoint on the map and define a region based on driving distance around that point. I was wondering if there was a way to search my database by that user defined region.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末蓝 2024-12-10 12:29:49

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

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

For the International System that uses Kilometers, replace a part of the script with this one:

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

Source: http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe#MySQL

如若梦似彩虹 2024-12-10 12:29:49

您应该能够获取图钉掉落的经度和纬度坐标,然后将该数据发送到您的服务器并使用此页面上列出的查询返回结果:http://code.google.com/apis/maps/articles/phpsqlsearch.html

选择 id, ( 3959 * acos(余弦(弧度(37) ) * cos( 弧度( 纬度 ) ) * cos( 弧度( lng ) - 弧度(-122) ) + sin( 弧度(37) ) * sin( 弧度( 纬度 ) ) ) ) AS 与标记的距离距离< 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/

You should be able to get the longitude and latitude coordinates of the pin drop, then send that data to your server and return a result using the query listed on this page: 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;

You would need to geocode your addresses into longitude and latitudes as well, and store them with each address though. This is the exact code I used to scrape longitude and latitudes for addresses in my database:

  /* 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 function: http://www.bin-co.com/php/scripts/xml2array/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文