0

This should be quite simple but I am not getting it.

I have a database of locations with lon/lat specified.

After loading the bing map I get the bounds

var view = map.getBounds();

and then call a webmethod to get all the locations which should be shown (within the bounds of the visible map).

I cannot figure out a query to get the locations (which all have a lon/lat specified) .

This obviously does NOT work as when negative values come into play they mess up the query:

SELECT Location_name, longtitude, latitude  FROM location_view WHERE latitude< '40.112'   and latitude> '35.783'   and longtitude< '28.10453' and longtitude> '19.315' 

Is there a normalized way to do this? So the comparison would work?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Bob
  • 31
  • 4
  • Your query will work absolutely fine with negative values: a longitude of -130 is still west of a longitude of -120. The only situation in which it won't work is if the bounds of your map crosses the 180th meridian... i.e. the "westmost" longitude is 170 and the "eastmost" latitude is -170. – Alastair Aitchison Mar 10 '12 at 19:29

1 Answers1

0

Your query will work absolutely fine with negative values: a longitude of -130 is still west of a longitude of -120. The only situation in which it won't work is if the bounds of your map crosses the 180th meridian. I.e. the "westmost" longitude is 170 and the "eastmost" latitude is -170.

What database are you using? If you're using SQL Server then you can define each of your locations as a Point using the geography datatype. The geography datatype operates on a round model of the earth, so it will account correctly for crossing the 180th meridian with a query like this:

SELECT Location_name
FROM location_view
WHERE location.STIntersects('POLYGON((19.315 35.783, 28.10453 35.783, 28.10453 40.112, 19.315 40.112, 19.315 35.783))') = 1;
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Alastair Aitchison
  • 3,532
  • 1
  • 16
  • 15
  • Thanks for the reply . I had already done a workarround by normalizing all values to >0 , ie. Adding to latitude+90 and to lon+180 . I am using MSSQSL and the datatype you refer to is not available. Although it does seem as a much cleaner approach than mine. – Bob Mar 11 '12 at 12:23
  • The datatype you mention is in 2008 and we are using sql2005. (sorry did not check before my comment) – Bob Mar 11 '12 at 12:33