在ANSI SQL兼容数据库中存储纬度或经度数据时,哪种数据类型最合适?应该float使用,或decimal,或......?
我知道Oracle,MySql和SQL Server已经添加了一些专门用于处理地理数据的特殊数据类型,但我对如何将信息存储在"普通的"SQL数据库中感兴趣.
我有一个纬度和经度:"-27.0000,133.0000".我希望根据它制作一个地图.
我试过去看看这个链接
https://maps.googleapis.com/maps/api/geocode/json?latlng=-27.0000,133.0000&key=******
我一直在浏览器上收到此错误
{
"error_message" : "This API project is not authorized to use this API. Please ensure that this API is activated in the APIs Console: Learn more: https://code.google.com/apis/console",
"results" : [],
"status" : "REQUEST_DENIED"
}
Run Code Online (Sandbox Code Playgroud)
但我想我已经启用了这个API.我登录到我的Google控制台,然后仔细检查.
当我去: https://console.developers.google.com/project/75423435770063/apiui/apis/enabled
我知道了 :
启用地理编码+地理定位.
我现在有点卡住了.我在这里错过了什么吗?
例如,如果我们有这些坐标集
"latitude": 48.858844300000001,
"longitude": 2.2943506,
Run Code Online (Sandbox Code Playgroud)
我们怎样才能找到这个城市/国家?
嗨,我需要计算具有lat和long的两个点之间的距离.
我想避免任何外部API调用.
我尝试在PHP中实现Haversine公式:
这是代码:
class CoordDistance
{
public $lat_a = 0;
public $lon_a = 0;
public $lat_b = 0;
public $lon_b = 0;
public $measure_unit = 'kilometers';
public $measure_state = false;
public $measure = 0;
public $error = '';
public function DistAB()
{
$delta_lat = $this->lat_b - $this->lat_a ;
$delta_lon = $this->lon_b - $this->lon_a ;
$earth_radius = 6372.795477598;
$alpha = $delta_lat/2;
$beta = $delta_lon/2;
$a = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($this->lat_a)) * cos(deg2rad($this->lat_b)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ;
$c = …Run Code Online (Sandbox Code Playgroud) 我正在计算两个GeoCoordinates之间的距离.我正在针对其他3-4个应用测试我的应用.当我计算距离时,我计算得到的平均值为3.3英里,而其他应用程序的距离为3.5英里.这与我正在尝试执行的计算有很大不同.有没有好的类库来计算距离?我在C#中计算它是这样的:
public static double Calculate(double sLatitude,double sLongitude, double eLatitude,
double eLongitude)
{
var radiansOverDegrees = (Math.PI / 180.0);
var sLatitudeRadians = sLatitude * radiansOverDegrees;
var sLongitudeRadians = sLongitude * radiansOverDegrees;
var eLatitudeRadians = eLatitude * radiansOverDegrees;
var eLongitudeRadians = eLongitude * radiansOverDegrees;
var dLongitude = eLongitudeRadians - sLongitudeRadians;
var dLatitude = eLatitudeRadians - sLatitudeRadians;
var result1 = Math.Pow(Math.Sin(dLatitude / 2.0), 2.0) +
Math.Cos(sLatitudeRadians) * Math.Cos(eLatitudeRadians) *
Math.Pow(Math.Sin(dLongitude / 2.0), 2.0);
// Using 3956 as the number of miles around the …Run Code Online (Sandbox Code Playgroud) 我尝试实现这个公式:http ://andrew.hedges.name/experiments/haversine/ aplet对我测试的两个点有好处:

但我的代码不起作用.
from math import sin, cos, sqrt, atan2
R = 6373.0
lat1 = 52.2296756
lon1 = 21.0122287
lat2 = 52.406374
lon2 = 16.9251681
dlon = lon2 - lon1
dlat = lat2 - lat1
a = (sin(dlat/2))**2 + cos(lat1) * cos(lat2) * (sin(dlon/2))**2
c = 2 * atan2(sqrt(a), sqrt(1-a))
distance = R * c
print "Result", distance
print "Should be", 278.546
Run Code Online (Sandbox Code Playgroud)
它返回的距离是5447.05546147.为什么?
我需要一个基于我所拥有的纬度/经度值的集合的国家,州和城市列表.我需要以保持层次结构并且没有重复的方式存储此信息(例如"USA"和"United States"和"United States of America"是同一个国家;我只想在我的数据库中有一个这个国家的实例) .
这可能与Google Map API有关吗?
我正在重新设计一个客户数据库,我想要存储的一条新信息以及标准地址字段(街道,城市等)是地址的地理位置.我想到的唯一用例是允许用户在无法找到地址时映射Google地图上的坐标,这通常发生在该地区是新开发的,或者位于偏远/乡村地区.
我的第一个倾向是将纬度和经度存储为十进制值,但后来我记得SQL Server 2008 R2有一个geography数据类型.我绝对没有使用过的经验geography,从我最初的研究来看,它看起来对我的场景来说太过分了.
例如,要使用存储为的纬度和经度decimal(7,4),我可以这样做:
insert into Geotest(Latitude, Longitude) values (47.6475, -122.1393)
select Latitude, Longitude from Geotest
Run Code Online (Sandbox Code Playgroud)
但是geography,我会这样做:
insert into Geotest(Geolocation) values (geography::Point(47.6475, -122.1393, 4326))
select Geolocation.Lat, Geolocation.Long from Geotest
Run Code Online (Sandbox Code Playgroud)
虽然它不是那么复杂,但如果我不需要增加复杂性呢?
在我放弃使用的想法之前geography,有什么我应该考虑的吗?使用空间索引搜索位置与使用纬度和经度字段编制索引会更快吗?使用geography我有什么优势我不知道?或者,另一方面,我应该知道哪些会阻止我使用geography?
@Erik Philips提出了进行近距离搜索的能力geography,非常酷.
另一方面,快速测试显示,使用select时获得经纬度的简单程度明显较慢geography(详情如下).并且对另一个SO问题的接受答案的评论geography让我很谨慎:
@SaphuA欢迎你.作为旁注,请非常小心在可空的GEOGRAPHY数据类型列上使用空间索引.存在一些严重的性能问题,因此即使您必须重新构建模式,也要使GEOGRAPHY列不可为空. - 托马斯6月18日11:18
总而言之,权衡接近搜索的可能性与性能和复杂性之间的权衡,我决定放弃geography在这种情况下的使用.
我跑的测试细节:
我创建了两个表,一个使用geography,另一个使用decimal(9,6)纬度和经度:
CREATE TABLE [dbo].[GeographyTest]
(
[RowId] …Run Code Online (Sandbox Code Playgroud) 我给出了一个由纬度和经度定义的位置.现在我想计算一个距离该点10公里的边界框.
边界框应定义为latmin,lngmin和latmax,lngmax.
我需要这些东西才能使用panoramio API.
有人知道如何获得积分的公式吗?
编辑:伙计们我正在寻找一个公式/函数,它以lat&lng作为输入并返回一个边界框作为latmin&lngmin和latmax&latmin.Mysql,php,c#,javascript很好,但伪代码也应该没问题.
编辑:我不是在寻找能够显示2点距离的解决方案
使用谷歌地理编码V3,如果我尝试地理编码20个地址,除非我次地为〜间隔1秒,我得到一个OVER_QUERY_LIMIT,但随后它需要20秒全部放在我的标记之前.
除了预先存储坐标之外,还有其他方法吗?
geocoding ×10
geolocation ×3
google-maps ×3
geo ×2
c# ×1
coordinates ×1
database ×1
geography ×1
geospatial ×1
haversine ×1
javascript ×1
panoramio ×1
php ×1
python ×1
sql ×1