【发布时间】:2010-03-06 18:08:26
【问题描述】:
我正在使用 Google 本地搜索进行一些地理编码,并且需要能够在页面上设置 google maps api v3 对象的视口。
现在,如果 google 本地搜索返回给我一个很好的视口,就像它对邮政编码或街道所做的那样,但如果我输入酒吧的名称,这很好。返回的视口可能很大,因为本地搜索找到了许多匹配项,但返回了最好的。
我想在这种情况下计算我自己的视口,并按以下方式计算:
result.Centre = new LatLon(double.Parse(lat),double.Parse(lon));
result.Span = new LatLon(0.006295d, 0.008407d);
string viewport = "{{\"center\":{{\"lat\":\"{0}\",\"lng\":\"{1}\"}},\"span\":{{\"lat\":\"{2}\",\"lng\":\"{3}\"}},\"sw\":{{\"lat\":\"{4}\",\"lng\":\"{5}\"}},\"ne\":{{\"lat\":\"{6}\",\"lng\":\"{7}\"}}}}";
//Calculate the south west by taking half the span and adding it to the centre
double swLat = result.Centre.Latitude + (result.Span.Latitude/2);
double swLon = result.Centre.Longitude + (result.Span.Longitude/2);
//and northeast corners by taking half the span and subtracting from the centre
double neLat = result.Centre.Latitude - (result.Span.Latitude/2);
double neLon = result.Centre.Longitude - (result.Span.Longitude/2);
//Fingers crossed :)
result.ViewPortJSON = string.Format(viewport, result.Centre.Latitude, result.Centre.Longitude,result.Span.Latitude, result.Span.Longitude, swLat, swLon,neLat, neLon);
问题是我得到了一些有效的 json,但谷歌地图缩小了,所以我可以看到整个世界。前端使用了相同的代码,就好像谷歌给了我视口一样,所以我不明白发生了什么。
有什么想法吗?我的计算有误吗?我已经尝试过整个跨度(即假设它已经是一个半径)具有相同的效果。
提前致谢。
【问题讨论】:
标签: c# api google-maps geocoding