计算谷歌地图的视口? [英] Calculating Google map's viewport?

查看:37
本文介绍了计算谷歌地图的视口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Google 本地搜索进行一些地理编码,并且需要能够在页面上设置 google maps 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.

现在,如果谷歌本地搜索为我返回了一个很好的视口,就像对邮政编码或街道一样,但如果我输入了酒吧的名称,这很好.返回的视口可能很大,因为本地搜索找到了很多匹配项,但返回了最好的.

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,但谷歌地图会缩小,所以我可以看到整个世界.前端使用了相同的代码,就好像谷歌给了我视口一样,所以我不明白发生了什么.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆