使用 Google Places API 获取给定地点的附近城市

发布于 2024-12-01 10:30:50 字数 1539 浏览 1 评论 0原文

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

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

发布评论

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

评论(2

染年凉城似染瑾 2024-12-08 10:30:50

我编写了一个代码片段,可让您通过组合 Google Maps Geocoding APIGeoNames.org API(除了 file_get_contents)来检索所有附近的城市strong>您还可以执行cURL请求)。

/*
 * Get cities based on city name and radius in KM
 */

// get geocode object as array from The Google Maps Geocoding API
$geocodeObject = json_decode(file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address={CITY NAME},{COUNTRY CODE}'), true);

// get latitude and longitude from geocode object
$latitude = $geocodeObject['results'][0]['geometry']['location']['lat'];
$longitude = $geocodeObject['results'][0]['geometry']['location']['lng'];

// set request options
$responseStyle = 'short'; // the length of the response
$citySize = 'cities15000'; // the minimal number of citizens a city must have
$radius = 30; // the radius in KM
$maxRows = 30; // the maximum number of rows to retrieve
$username = '{YOUR USERNAME}'; // the username of your GeoNames account

// get nearby cities based on range as array from The GeoNames API
$nearbyCities = json_decode(file_get_contents('http://api.geonames.org/findNearbyPlaceNameJSON?lat='.$latitude.'&lng='.$longitude.'&style='.$responseStyle.'&cities='.$citySize.'&radius='.$radius.'&maxRows='.$maxRows.'&username='.$username, true));

// foreach nearby city get city details
foreach($nearbyCities->geonames as $cityDetails)
{
    // do something per nearby city
}

请注意您的请求量,因为 API 是有限的

有关 API 的详细信息,请访问以下网址:

I've written a code snippet that allows you to retrieve all nearby cities by combining the Google Maps Geocoding API and GeoNames.org API (besides a file_get_contents your could also do a cURL request).

/*
 * Get cities based on city name and radius in KM
 */

// get geocode object as array from The Google Maps Geocoding API
$geocodeObject = json_decode(file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address={CITY NAME},{COUNTRY CODE}'), true);

// get latitude and longitude from geocode object
$latitude = $geocodeObject['results'][0]['geometry']['location']['lat'];
$longitude = $geocodeObject['results'][0]['geometry']['location']['lng'];

// set request options
$responseStyle = 'short'; // the length of the response
$citySize = 'cities15000'; // the minimal number of citizens a city must have
$radius = 30; // the radius in KM
$maxRows = 30; // the maximum number of rows to retrieve
$username = '{YOUR USERNAME}'; // the username of your GeoNames account

// get nearby cities based on range as array from The GeoNames API
$nearbyCities = json_decode(file_get_contents('http://api.geonames.org/findNearbyPlaceNameJSON?lat='.$latitude.'&lng='.$longitude.'&style='.$responseStyle.'&cities='.$citySize.'&radius='.$radius.'&maxRows='.$maxRows.'&username='.$username, true));

// foreach nearby city get city details
foreach($nearbyCities->geonames as $cityDetails)
{
    // do something per nearby city
}

be carefull with your requests amount because the API's are limited

For more information about the API's visit the following url's:

梦幻的心爱 2024-12-08 10:30:50

您不想使用 Places API 的说法可能是正确的。您可能想看看是否有一种方法可以使用地理编码/反向地理编码来获得您想要的东西。请参阅 http://code.google.com/apis/maps/documentation/geocoding/< /a>.

根据您的性能和准确性要求,您可能无法使用 Google Maps API(或任何其他免费工具)轻松地做到这一点(尤其是在全球范围内),但您可能可以获得一些基本有效的东西,特别是如果您有地理限制(例如,仅限美国),这可能会简化事情。

You are likely correct that you will not want to use the Places API. You may want to see if there is a way to use geocoding/reverse geocoding to get what you want. See http://code.google.com/apis/maps/documentation/geocoding/.

Depending on your performance and accuracy requirements, you may not be able to easily do this (especially globally) with Google Maps API (or any other free tool), but you might be able to get something that sorta kinda mostly-works, especially if you have a geographical restriction (e.g., just the U.S.) that might simplify things.

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