计算谷歌地图的视口? [英] Calculating Google map's viewport?
问题描述
我做了一些使用谷歌本地搜索的地理编码,并需要能够设置一个谷歌地图API V3对象的视口的页面上。
I'm doing a bit of geocoding using Google Local Search and need to be able to set the viewport of a google maps api v3 object on the page.
现在这是好的,如果因为它为后code或街道谷歌本地搜索返回我一个很好的视口,但如果我把例如一个酒吧的名字。返回视口是非常大的presumable因为本地搜索已经发现了很多的比赛,但已经恢复最好的。
Now this is fine if google local search has returned me a good viewport as it does for a postcode or street but if i put in the name of a bar for example. The viewport returned is very large presumable because local search has found many matches but has returned the best.
我想计算在这种情况下我自己的视口,我在下面的方式这样做的:
I'd like to calculate my own viewport in this case and am doing so in the following manner:
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但谷歌地图会放大右出,所以我可以看到整个世界。同样的code在前端被用作如果谷歌给我的视口,以便我不明白是怎么回事。
The problem is I get some valid json but google maps zooms right out so I can see the whole world. The same code is being used at the front end as if google has given me the viewport so I don't understand what is going on.
任何想法?有我有我的算盘打错了?我曾尝试与整个跨度(即假设它已经是一个半径)具有同样的效果。
Any ideas? Have I got my calculations wrong? I have tried with the whole span (i.e. assuming it is already a radius) with the same effect.
先谢谢了。
推荐答案
哎,你不应该做:
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);
这篇关于计算谷歌地图的视口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!