How can I only show new markers that are only in a certain radius around the center?

213 views
Skip to first unread message

ArnMan

unread,
Jul 21, 2011, 8:35:15 AM7/21/11
to google-map...@googlegroups.com
I am working on a page that has a custom google map, that checks mysql for current longitude and latitude, and centers the map on that.
I would like to be able to show markers that are a certain distance away from the center marker, from other locations in the database
I have a mysql database that has locations. and another table that includes the center location, which changes. I currently have a little green marker that shows where the center location is, and moves when the long and lat changes.
Example.
Lets say I am at 0/0 Long/Lat
and I want only the markers shown if their coordinates are within
5/5
-5/5
-5/-5
5/-5

Is there anyway to use php, or javascript to check inside those coordinates everytime the page loads and place a marker where is a location?


Thank you in advance




en4ce

unread,
Jul 21, 2011, 9:38:53 AM7/21/11
to Google Maps JavaScript API v3
you might use this php wise: (untested)

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) *
cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) *
sin( radians( lat ) ) ) ) AS distance
FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;


Here's the SQL statement that will find the closest 20 locations that
are within a radius of 25 miles to the 37, -122 coordinate. It
calculates the distance based on the latitude/longitude of that row
and the target latitude/longitude, and then asks for only rows where
the distance value is less than 25, orders the whole query by
distance, and limits it to 20 results. To search by kilometers instead
of miles, replace 3959 with 6371.


On 21 Jul., 14:35, ArnMan <arnman2...@gmail.com> wrote:
> I am working on a page that has a custom google map, that checks mysql for
> current longitude and latitude, and centers the map on that.
> I would like to be able to show markers that are a certain distance away
> from the center marker, from other locations in the database
> I have a mysql database that has locations. and another table that includes
> the center location, which changes. I currently have a little green marker
> that shows where the center location is, and moves when the long and lat
> changes.
> Example.
> Lets say I am at 0/0 Long/Lat
> and I want only the markers shown if their coordinates are within
> 5/5
> -5/5
> -5/-5
> 5/-5
>
> <https://lh6.googleusercontent.com/-05j9Ez9JLtY/Tigb_FlphjI/AAAAAAAAAB...>

Francis Carriere

unread,
Jul 21, 2011, 12:44:00 PM7/21/11
to google-map...@googlegroups.com
Might not be 100% exactly what you need, but this should get you started in the right direction. 

//Check if within range      
        var R = 6371; // km
        var dLat = (curLat-paramLat).toRad();
        var dLon = (curLng-paramLong).toRad(); 
        var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(paramLat.toRad()) * Math.cos(paramLong.toRad()) * 
                Math.sin(dLon/2) * Math.sin(dLon/2); 
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
        var d = R * c;
      
        if(d <= (paramRadius/1000)){   
            coordList.push({       
            lat: curLat, 
            lng: curLng, 
            alt: parseFloat(coords[2])
          });

xelawho

unread,
Jul 21, 2011, 3:14:24 PM7/21/11
to Google Maps JavaScript API v3
> Is there anyway to use php, or javascript to check inside those coordinates
> everytime the page loads and place a marker where is a location?

couldn't you just computeDistanceBetween() for the markers and the
center and if the distance is less than the search radius, make the
marker visible?
Reply all
Reply to author
Forward
0 new messages