Computing a Google Map Viewer Window?

I am doing a bit of geocoding using Google Local Search and should be able to set the google maps api v3 object on the page on the watch page.

Now it’s ok if a local google search returned me a good viewport, like for a zip code or street, but if I typed the name of the bar, for example. The returned viewport is very large because the local search found many matches, but returned the best.

I would like to calculate my own viewport in this case and do it as follows:

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

The problem is that I get some valid json, but Google Maps scales directly so that I can see the whole world. The same code is used in the interface, as if Google gave me a viewport, so I don’t understand what is happening.

Any ideas? I misunderstood my calculations? I tried the whole range (i.e. Assuming that this is already a radius) with the same effect.

Thanks in advance.

0
source share
1 answer

Hey you shouldn't do:

 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); 
+1
source

Source: https://habr.com/ru/post/1480218/


All Articles