Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8431751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:56:24+00:00 2026-06-10T05:56:24+00:00

Using google maps I would like to retrieve and display all locations withing a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:56:25+00:00Added an answer on June 10, 2026 at 5:56 am

    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.

    // PHP/MySQL code
    $sourceLat = '';
    $sourceLon = '';
    $radiusKm  = 50;
    
    $proximity = mathGeoProximity($sourceLat, $sourceLon, $radiusKm);
    $result    = mysql_query("
        SELECT * 
        FROM   locations
        WHERE  (lat BETWEEN " . number_format($proximity['latitudeMin'], 12, '.', '') . "
                AND " . number_format($proximity['latitudeMax'], 12, '.', '') . ")
          AND (lon BETWEEN " . number_format($proximity['longitudeMin'], 12, '.', '') . "
                AND " . number_format($proximity['longitudeMax'], 12, '.', '') . ")
    ");
    
    // fetch all record and check wether they are really within the radius
    $recordsWithinRadius = array();
    while ($record = mysql_fetch_assoc($result)) {
        $distance = mathGeoDistance($sourceLat, $sourceLon, $record['lat'], $record['lon']);
    
        if ($distance <= $radiusKm) {
            $recordsWithinRadius[] = $record;
        }
    }
    
    // and then print your results using a google map
    // ...
    
    
    // calculate geographical proximity
    function mathGeoProximity( $latitude, $longitude, $radius, $miles = false )
    {
        $radius = $miles ? $radius : ($radius * 0.621371192);
    
        $lng_min = $longitude - $radius / abs(cos(deg2rad($latitude)) * 69);
        $lng_max = $longitude + $radius / abs(cos(deg2rad($latitude)) * 69);
        $lat_min = $latitude - ($radius / 69);
        $lat_max = $latitude + ($radius / 69);
    
        return array(
            'latitudeMin'  => $lat_min,
            'latitudeMax'  => $lat_max,
            'longitudeMin' => $lng_min,
            'longitudeMax' => $lng_max
        );
    }
    
    // calculate geographical distance between 2 points
    function mathGeoDistance( $lat1, $lng1, $lat2, $lng2, $miles = false )
    {
        $pi80 = M_PI / 180;
        $lat1 *= $pi80;
        $lng1 *= $pi80;
        $lat2 *= $pi80;
        $lng2 *= $pi80;
    
        $r = 6372.797; // mean radius of Earth in km
        $dlat = $lat2 - $lat1;
        $dlng = $lng2 - $lng1;
        $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
        $km = $r * $c;
    
        return ($miles ? ($km * 0.621371192) : $km);
    }
    

    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

    // add this to the above php script
    header('Content-type: application/json');
    echo json_encode( $recordsWithinRadius );
    exit();
    

    UPDATE: How to load the json via an AJAX call in jquery

    // javascript/jquery code
    $(document).ready(function()
    {
        $.getJSON('http://yourserver/yourscript.php', function(data)
        {
            $.each(data, function(key, record) {
                // do something with record data
                console.log(record);
            });
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the Google Maps API and would like it to render a very
I'm using the Google Maps API (v2) and would like to center the map
I have a website using the Google-maps Javascript API V3. I would like to
Hi I have an iPhone application using maps and locations. I would like the
I am using a google maps custom control. I would like a text link
I am using Google maps API version 3. I would like my double click
When using Google Maps API with Google Fusion Tables (with API), I would like
I am using Google Maps API V3, I would like to know how I
It seems that licensing terms would prevent us from using Google Maps API in
I am using Google Maps V3 API to display my map. Problem: How can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.