例如,如果我们有这些坐标集
"latitude": 48.858844300000001,
"longitude": 2.2943506,
我们如何找出城市/国家?
例如,如果我们有这些坐标集
"latitude": 48.858844300000001,
"longitude": 2.2943506,
我们如何找出城市/国家?
Answers:
免费的Google Geocoding API通过HTTP REST API提供此服务。请注意,该API的使用和速率受到限制,但是您可以为无限制的访问付费。
尝试此链接以查看输出示例(这在json中,输出在XML中也可用)
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true
另外的选择:
优点:
缺点:
你需要 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'}
开源替代品是《开放街道地图》中的Nominatim。您要做的就是在URL中设置变量,它返回该位置的城市/国家。请检查以下链接以获取官方文档:名义上
我正在寻找类似的功能,并且在较早的答复中看到了共享的数据“ http://download.geonames.org/export/dump/(感谢您的共享,它是一个很好的来源),并实现了基于在city1000.txt数据上。
您可以看到它在运行 
http://scatter-otl.rhcloud.com/location?lat=36&long=-78.9 (链接断开) 
只需更改您所在位置的经度和纬度即可。
它部署在OpenShift(RedHat平台)上。长时间的闲置时间过后,首次呼叫可能需要一段时间,但通常性能令人满意。随意使用此服务...
另外,您可以在https://github.com/turgos/Location中找到项目源 。
我使用了Geocoder,这是一个很好的Python库,它支持多个提供程序,包括Google,Geonames和OpenStreetMaps,仅举几个例子。我尝试使用GeoPy库,但它经常会超时。为GeoNames开发自己的代码并不是最好的时间选择,并且最终可能会获得不稳定的代码。根据我的经验,Geocoder非常易于使用,并且具有足够的文档。以下是一些示例代码,用于按纬度和经度查找城市,或按城市名称查找纬度/经度。
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
geocoder,我也想自己尝试一下!
                    我花了大约30分钟的时间来尝试找到一个代码示例,说明如何在Javascript中执行此操作。对于您发布的问题,我找不到快速明确的答案。所以...我做了我自己的。希望人们可以使用它,而不必深入研究API或盯着他们不知道如何阅读的代码。哈,如果没有别的,我可以参考这篇文章作为我自己的东西。.很好的问题,并感谢论坛的讨论!
这是利用Google 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>
。
//CHECK IF BROWSER HAS HTML5 GEO LOCATION
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (position) {
        //GET USER CURRENT LOCATION
        var locCurrent = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        //CHECK IF THE USERS GEOLOCATION IS IN AUSTRALIA
        var geocoder = new google.maps.Geocoder();
            geocoder.geocode({ 'latLng': locCurrent }, function (results, status) {
                var locItemCount = results.length;
                var locCountryNameCount = locItemCount - 1;
                var locCountryName = results[locCountryNameCount].formatted_address;
                if (locCountryName == "Australia") {
                    //SET COOKIE FOR GIVING
                    jQuery.cookie('locCountry', locCountryName, { expires: 30, path: '/' }); 
                }
        });
    }
}
这实际上取决于您所拥有的技术限制。
一种方法是拥有一个空间数据库,其中包含您感兴趣的国家和城市的轮廓。通过轮廓,我的意思是将国家和城市存储为空间类型多边形。可以将您的一组坐标转换为空间类型的点,然后对多边形进行查询,以获取该点所在的国家/城市名称。
以下是一些支持空间类型的数据库:SQL Server 2008,MySQL,postGIS -postgreSQL和Oracle的扩展。
如果您想使用服务而不是拥有自己的数据库,则可以使用Yahoo的GeoPlanet。对于服务方法,您可能需要在gis.stackexchange.com上查看此答案,其中涵盖了用于解决问题的服务的可用性。
您可以使用Google Geocoding API
Bellow是返回地址,城市,州和国家/地区的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;
}
Loc2country是一个基于Golang的工具,可返回给定位置坐标(纬度/经度)的ISO alpha-3国家/地区代码。它以微秒为单位响应。它使用地理哈希到国家地图。
geohash数据是使用georaptor生成的。
我们在此工具的第6级使用geohash,即大小为1.2km x 600m的盒子。
最小化库的数量。
在他们的网站上获取使用api的密钥,然后在http请求中获得结果:
curl -i -H“键:YOUR_KEY” -X GET https://api.latlong.dev/lookup?lat=38.7447913&long=-9.1625173
请检查以下答案。这个对我有用
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;
}
如果您使用的是Google的Places API,则可以通过以下方法使用Javascript从place对象获取国家和城市:
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;
  }
}