给定纬度/经度坐标,我们如何找出城市/国家/地区?

| 例如,如果我们有这些坐标集
\"latitude\": 48.858844300000001,
\"longitude\": 2.2943506,
我们如何找出城市/国家?     
已邀请:
        免费的Google Geocoding API通过HTTP REST API提供此服务。请注意,API受使用和速率限制,但是您可以为无限制访问付费。 尝试此链接以查看输出示例(这在json中,输出在XML中也可用) https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true     
        另外的选择: 从http://download.geonames.org/export/dump/下载城市数据库 将每个城市作为纬度/经度->城市映射添加到空间索引(例如R-Tree)(某些DB也具有此功能) 使用最近邻搜索来查找任何给定点的最近城市 优点: 不依赖于外部服务器是否可用 非常快(每秒轻松进行数千次查询) 缺点: 不会自动更新 如果要区分最近的城市在几十英里之外的情况,则需要额外的代码 可能在两极和国际日期变更线附近产生奇怪的结果(尽管这些地方没有任何城市     
        你需要
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\'}
    
        我正在寻找类似的功能,并且在较早的答复中看到了共享的数据“ 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/位置。     
        一个开源替代方案是《开放街道地图》中的Nominatim。 您所要做的就是在URL中设置变量,它返回该位置的城市/国家。请检查以下链接以获取官方文档:名义上     
        我使用了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
    
        我花了大约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 \'s GeoPlanet。对于服务方法,您可能需要在gis.stackexchange.com上查看此答案,其中涵盖了解决问题的服务可用性。     
我知道这个问题确实很老,但是我一直在研究同一个问题,所以我发现了一个非常有效和便捷的软件包,
reverse_geocoder
,由Ajay Thampi制作。 该代码在此处可用。它基于K-D树的并行实现,这对于大量点非常有效(我花了几秒钟才获得100,000点。 它基于此数据库,已经由@turgos突出显示。 如果您的任务是快速找到坐标列表的国家和城市,这是一个很好的工具。     
        您可以使用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的盒子。     
        请检查以下答案。这个对我有用
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;
  }
}
    

要回复问题请先登录注册