Using google maps I would like to retrieve and display all locations withing a fixed radius of a given point.
I’ve managed to find Display Location guide and also saw numerous posts regarding retrieving it using SQL query.
My database containing, name of the item, address (to find alt,lon), alt, lon and description.
How do I use the stored alt, lon to retrieve only the ones in, let’s say 50km radius.
Here is my code:
javascript
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(31.046051, 34.85161199999993);
var myOptions = {
zoom: 7,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
function updateCoordinates(latlng)
{
if(latlng)
{
document.getElementById('lat').value = latlng.lat();
document.getElementById('lng').value = latlng.lng();
}
}
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
updateCoordinates(results[0].geometry.location);
if (marker) marker.setMap(null);
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
draggable: true
});
google.maps.event.addListener(marker, "dragend", function() {
updateCoordinates(marker.getPosition());
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
function showPositionCoupons(sentlat, sentlon)
{
lat=sentlat;
lon=sentlon;
latlon=new google.maps.LatLng(lat, lon)
mapholder=document.getElementById('map_canvas')
var myOptions={
center:latlon,zoom:14,
mapTypeId:google.maps.MapTypeId.ROADMAP,
mapTypeControl:false,
};
map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
marker = new google.maps.Marker({position:latlon,map:map,title:"You are here!"});
}
I think I need to use showPositionCoupons() in a loop maybe, while reading alt and lon?
Thanks for any help given, I know it’s frequent question yet I couldn’t manage to solve it using what exists.
I’ve tried to use Display Location guide DisplayLocations() method, yet it didn’t work for me, though the way the locations presented there is perfect, only need to exclude the ones out of radius bounds.
Might not exactly be what you where expecting, but I hope it might help nonetheless.
Going from the link you’ve posted, I take you’re using PHP/MySQL.
And if that’s true, I would just use PHP/MySQL to fetch the correct results, and then afterwards display them on a Google map.
Much more efficient if you can do the calculations without needing an external service.
And then do with the results what you want. You can even implement this solution via an AJAX call if you’d need it on the fly.
UPDATE: How to output records as json
UPDATE: How to load the json via an AJAX call in jquery