Geolocation 给定纵横坐标,我们如何才能找到城市/国家?
例如,如果我们有一组坐标Geolocation 给定纵横坐标,我们如何才能找到城市/国家?,geolocation,geocoding,geospatial,geo,Geolocation,Geocoding,Geospatial,Geo,例如,如果我们有一组坐标 "latitude": 48.858844300000001, "longitude": 2.2943506, 我们如何才能了解城市/国家?这实际上取决于您有哪些技术限制 一种方法是建立一个包含你感兴趣的国家和城市轮廓的空间数据库。我所说的大纲是指国家和城市存储为空间类型多边形。可以将坐标集转换为空间类型点,并根据多边形进行查询,以获取点所在的国家/城市名称 以下是一些支持空间类型的数据库:,-postgreSQL的扩展和 如果你想使用一项服务,而不是拥有自己的数据库
"latitude": 48.858844300000001,
"longitude": 2.2943506,
我们如何才能了解城市/国家?这实际上取决于您有哪些技术限制 一种方法是建立一个包含你感兴趣的国家和城市轮廓的空间数据库。我所说的大纲是指国家和城市存储为空间类型多边形。可以将坐标集转换为空间类型点,并根据多边形进行查询,以获取点所在的国家/城市名称 以下是一些支持空间类型的数据库:,-postgreSQL的扩展和 如果你想使用一项服务,而不是拥有自己的数据库,你可以使用雅虎的。对于您可能希望查看答案的服务方法,其中包括解决您的问题的服务的可用性 免费软件通过HTTP REST API提供此服务。请注意,API是免费的,但您可以为无限访问付费 尝试此链接以查看输出示例(这是json格式的,输出也可以是XML格式的) 另一个选项:
- 从下载城市数据库
- 将每个城市添加为一个lat/long->city映射到一个空间索引中,例如R树(一些数据库也具有此功能)
- 使用最近邻搜索查找任何给定点的最近城市
- 不依赖于可用的外部服务器
- 非常快(每秒可轻松进行数千次查找)
- 不自动更新
- 如果要区分最近的城市在几十英里之外的情况,则需要额外的代码
- 可能会在两极和国际日期线附近产生奇怪的结果(尽管这些地方没有任何城市)
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
initialize(position.coords.latitude,position.coords.longitude);
});
}
function initialize(lat,lng) {
//directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
//directionsService = new google.maps.DirectionsService();
var latlng = new google.maps.LatLng(lat, lng);
//alert(latlng);
getLocation(latlng);
}
function getLocation(latlng){
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var loc = getCountry(results);
alert("location is::"+loc);
}
}
});
}
function getCountry(results)
{
for (var i = 0; i < results[0].address_components.length; i++)
{
var shortname = results[0].address_components[i].short_name;
var longname = results[0].address_components[i].long_name;
var type = results[0].address_components[i].types;
if (type.indexOf("country") != -1)
{
if (!isNullOrWhitespace(shortname))
{
return shortname;
}
else
{
return longname;
}
}
}
}
function isNullOrWhitespace(text) {
if (text == null) {
return true;
}
return text.replace(/\s/gi, '').length < 1;
}
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(函数(位置){
初始化(位置.坐标.纬度,位置.坐标.经度);
});
}
功能初始化(lat、lng){
//directionsDisplay=新建google.maps.DirectionsRenderer(渲染器选项);
//directionsService=new google.maps.directionsService();
var latlng=新的google.maps.latlng(lat,lng);
//警报(latlng);
获取位置(latlng);
}
功能获取位置(latlng){
var geocoder=new google.maps.geocoder();
geocoder.geocode({'latLng':latLng},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[0]){
var loc=getCountry(结果);
警报(“位置为::”+loc);
}
}
});
}
功能getCountry(结果)
{
对于(var i=0;i
我花了大约30分钟的时间试图找到一个如何用Javascript实现这一点的代码示例。我找不到一个快速清晰的答案来回答你发布的问题。所以…我自己做了。希望人们不用深入API或盯着他们不知道如何阅读的代码就可以使用它。哈,如果没有其他东西,我可以参考这篇文章对于我自己的东西…很好的问题,谢谢论坛的讨论
这是利用谷歌API
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=<YOURGOOGLEKEY>&sensor=false&v=3&libraries=geometry"></script>
我正在搜索一个类似的功能,我在前面的回复中看到了共享的数据“”(感谢您的共享,这是一个很好的来源),并实现了一个基于cities1000.txt数据的服务 你可以看到它以最快的速度运行 (断开的链接) 只需更改您所在位置的纬度和经度 它部署在OpenShift(RedHat平台)上。长时间空闲后的第一次调用可能需要一些时间,但通常性能是令人满意的。 您可以随意使用此服务 此外,您还可以在以下位置找到项目源:
.您可以使用谷歌地理编码API 下面是返回地址、城市、州和国家的php函数
public function get_location($latitude='', $longitude='')
{
$geolocation = $latitude.','.$longitude;
$request = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.$geolocation.'&sensor=false';
$file_contents = file_get_contents($request);
$json_decode = json_decode($file_contents);
if(isset($json_decode->results[0])) {
$response = array();
foreach($json_decode->results[0]->address_components as $addressComponet) {
if(in_array('political', $addressComponet->types)) {
$response[] = $addressComponet->long_name;
}
}
if(isset($response[0])){ $first = $response[0]; } else { $first = 'null'; }
if(isset($response[1])){ $second = $response[1]; } else { $second = 'null'; }
if(isset($response[2])){ $third = $response[2]; } else { $third = 'null'; }
if(isset($response[3])){ $fourth = $response[3]; } else { $fourth = 'null'; }
if(isset($response[4])){ $fifth = $response[4]; } else { $fifth = 'null'; }
$loc['address']=''; $loc['city']=''; $loc['state']=''; $loc['country']='';
if( $first != 'null' && $second != 'null' && $third != 'null' && $fourth != 'null' && $fifth != 'null' ) {
$loc['address'] = $first;
$loc['city'] = $second;
$loc['state'] = $fourth;
$loc['country'] = $fifth;
}
else if ( $first != 'null' && $second != 'null' && $third != 'null' && $fourth != 'null' && $fifth == 'null' ) {
$loc['address'] = $first;
$loc['city'] = $second;
$loc['state'] = $third;
$loc['country'] = $fourth;
}
else if ( $first != 'null' && $second != 'null' && $third != 'null' && $fourth == 'null' && $fifth == 'null' ) {
$loc['city'] = $first;
$loc['state'] = $second;
$loc['country'] = $third;
}
else if ( $first != 'null' && $second != 'null' && $third == 'null' && $fourth == 'null' && $fifth == 'null' ) {
$loc['state'] = $first;
$loc['country'] = $second;
}
else if ( $first != 'null' && $second == 'null' && $third == 'null' && $fourth == 'null' && $fifth == 'null' ) {
$loc['country'] = $first;
}
}
return $loc;
}
开源的替代方案是来自开放街道地图的提名。
您只需在URL中设置变量,然后返回该位置的城市/国家/地区。请查看以下官方文档链接:您需要
geopy
pip install geopy
然后:
from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.reverse("48.8588443, 2.2943506")
print(location.address)
要获取更多信息,请执行以下操作:
print (location.raw)
{'place_id': '24066644', 'osm_id': '2387784956', 'lat': '41.442115', 'lon': '-8.2939909', 'boundingbox': ['41.442015', '41.442215', '-8.2940909', '-8.2938909'], 'address': {'country': 'Portugal', 'suburb': 'Oliveira do Castelo', 'house_number': '99', 'city_district': 'Oliveira do Castelo', 'country_code': 'pt', 'city': 'Oliveira, São Paio e São Sebastião', 'state': 'Norte', 'state_district': 'Ave', 'pedestrian': 'Rua Doutor Avelino Germano', 'postcode': '4800-443', 'county': 'Guimarães'}, 'osm_type': 'node', 'display_name': '99, Rua Doutor Avelino Germano, Oliveira do Castelo, Oliveira, São Paio e São Sebastião, Guimarães, Braga, Ave, Norte, 4800-443, Portugal', 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright'}
如果您使用的是Google的Places API,这就是使用Javascript从place对象获取country和city的方法:
function getCityAndCountry(location) {
var components = {};
for(var i = 0; i < location.address_components.length; i++) {
components[location.address_components[i].types[0]] = location.address_components[i].long_name;
}
if(!components['country']) {
console.warn('Couldn\'t extract country');
return false;
}
if(components['locality']) {
return [components['locality'], components['country']];
} else if(components['administrative_area_level_1']) {
return [components['administrative_area_level_1'], components['country']];
} else {
console.warn('Couldn\'t extract city');
return false;
}
}
函数getCityAndCountry(位置){
var组件={};
对于(变量i=0;ifunction getCityAndCountry(location) {
var components = {};
for(var i = 0; i < location.address_components.length; i++) {
components[location.address_components[i].types[0]] = location.address_components[i].long_name;
}
if(!components['country']) {
console.warn('Couldn\'t extract country');
return false;
}
if(components['locality']) {
return [components['locality'], components['country']];
} else if(components['administrative_area_level_1']) {
return [components['administrative_area_level_1'], components['country']];
} else {
console.warn('Couldn\'t extract city');
return false;
}
}
import geocoder
g = geocoder.osm([53.5343609, -113.5065084], method='reverse')
print g.json['city'] # Prints Edmonton
g = geocoder.osm('Edmonton, Canada')
print g.json['lat'], g.json['lng'] # Prints 53.5343609, -113.5065084
curl -i -H "key: YOUR_KEY" -X GET https://api.latlong.dev/lookup?lat=38.7447913&long=-9.1625173