Converting circle to LatLngBounds

1,093 views
Skip to first unread message

Nico

unread,
Jul 13, 2011, 7:14:57 AM7/13/11
to Google Maps JavaScript API v3
Hello,

my main intention with my application is to spot given point of
interests in the radius of a 5km circle. To do that I had to convert
the circle to a LatLngBounds Object, which doesn't seem to do the
trick.

For the purpose of testing I made a new Marker, with the latlng
coordinates of the north-east corner of the bound, but this marker is
totally out of the circle and I don't know why.

It would be very nice if someone could take a look at my sourcecode:

http://nicojensen.kilu.de/poiRecognition.html

Sorry for the bad english. I hope people understand what I want. :)

Nico

Andrew Leach

unread,
Jul 13, 2011, 7:55:06 AM7/13/11
to google-map...@googlegroups.com
On 13 July 2011 12:14, Nico <nico.j...@googlemail.com> wrote:
>
> my main intention with my application is to spot given point of
> interests in the radius of a 5km circle. To do that I had to convert
> the circle to a LatLngBounds Object, which doesn't seem to do the
> trick.

It won't, as you found.

> For the purpose of testing I made a new Marker, with the latlng
> coordinates of the north-east corner of the bound, but this marker is
> totally out of the circle and I don't know why.

The bounding box of a circle is a square which touches the circle at
four points. There will be areas in the corners of the square which
are outside the circle.

To find whether a point is within a circle, simply find the distance
between the point and the centre of the circle. If it's less than the
radius it must be inside the circle. You don't need bounding boxes at
all.

Nico

unread,
Jul 13, 2011, 8:32:11 AM7/13/11
to Google Maps JavaScript API v3
I already thought about something like that, but I don't know how to
implement that. How should I measure the distance between the two
points? Is there some kind of method I can use or do I have to
implement it myself somehow? In that case I probably have to convert
the measurements somehow .. Honestly I dont know where to start and it
would be very, very nice of you if you could give me a hint.

Andrew Leach

unread,
Jul 13, 2011, 8:49:09 AM7/13/11
to google-map...@googlegroups.com

Hint: http://code.google.com/apis/maps/documentation/javascript/geometry.html

var LL1=new google.maps.LatLng(...,...);
var LL2=new google.maps.LatLng(...,...);
var d=computeDistanceBetween(LL1,LL2);

Note that you need to explicitly load the Geometry library, it's not
part of the basic API.

Nico

unread,
Jul 13, 2011, 9:28:20 AM7/13/11
to Google Maps JavaScript API v3
Thanks for the help, but somehow it still doesn't work. Feeling really
stupid right now ...

I got my first coordinate via the Geolocation API and the second one I
defined myself .. which might be the problem.

var latlng = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var latlng2 = new google.maps.LatLng(51.43545462624757,
6.770839691162109);
var d = computeDistanceBetween(latlng,latlng2);

In the head I replaced the former googlemaps src with this one:

src="http://maps.googleapis.com/maps/api/js?
sensor=true&libraries=geometry"

Thanks in advance




Andrew Leach

unread,
Jul 13, 2011, 9:36:12 AM7/13/11
to google-map...@googlegroups.com
On 13 July 2011 14:28, Nico <nico.j...@googlemail.com> wrote:
> Thanks for the help, but somehow it still doesn't work. Feeling really
> stupid right now ...

Not enough information. What "doesn't work"? What errors are you seeing?

Please follow the posting guidelines and post a link. Can't debug a
code listing.

Nico

unread,
Jul 13, 2011, 9:51:49 AM7/13/11
to Google Maps JavaScript API v3
The page is still the same:

http://nicojensen.kilu.de/poiRecognition.html

As for errors. There is only one in the script tag:
unescaped & or unknown entity "&libraries"

Which doesnt make much sense, as I just copied it from an example,
that I found.
Reply all
Reply to author
Forward
0 new messages