2010-03-06 31 views
0

我正在使用Google本地搜索進行一些地理編碼,並且需要能夠在頁面上設置Google Maps API對象的視口。計算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但谷歌地圖會放大右出,所以我可以看到整個世界。在前端使用相同的代碼,就好像谷歌給了我視口,所以我不明白是怎麼回事。

任何想法?我的計算錯了嗎?我已經嘗試了整個跨度(即假設它已經是一個半徑)並且具有相同的效果。

在此先感謝。

回答

1

哎,你不應該做:

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); 
+0

沒錯!呃,我在考慮X和Y而不是經緯度。謝謝 :-) – 2010-03-07 19:06:08