Search code examples
google-mapsparsefloat

Geolocate issue - radius returned correctly, but no items returned


I am trying to get a geolocate function work within a wordpress install. Basically what happens is that any item within a radius x of the given location of the site visitor is being returned on a google map. This works fine but only if the radius is equal or bigger than 1 km. Whenever a given radius is smaller than 1 km the radius itself is shown correctly but the database items within this radius are not being displayed. Inicially I also had problems showing the correct radius smaller than 1 km, but I solved that by using parseFloat instead of parseInt. Unfortunately my skills are far from perfect when it comes to this and I could need some help of someone competent here. Please let me know if you have ideas. The two involved script are included here.

   if(geoloc){
            if(ajaxGeo){
                var inputRadius = geoInputRadius.val();
                if(!isNaN(inputRadius)){
                    var radiusInM = parseFloat(geoInputRadius.val()) * 1000;
                } else {
                    var radiusInM = parseInt(geoInputRadius.data('default-value')) * 1000;
                }
                // autofit by circle
                mapObj.circle = {
                    options: {
                        center: geoloc,
                        radius : radiusInM,
                        visible : {ifset $themeOptions->search->showAdvancedSearchRadius}true{else}false{/ifset},
                        fillOpacity : 0.15,
                        fillColor : "#2c82be",
                        strokeColor : "#2c82be"
                    }
                }
            } else {
                var radiusInM = parseInt({ifset $geolocationRadius}{$geolocationRadius}{else}100{/ifset}) * 1000;
                // autofit by circle
                mapObj.circle = {
                    options: {
                        center: geoloc,
                        radius : radiusInM,
                        visible : {ifset $geolocationCircle}true{else}false{/ifset},
                        fillOpacity : 0.15,
                        fillColor : "#2c82be",
                        strokeColor : "#2c82be"
                    }
                }
            }
        }

function isPointInRadius($radiusInKm, $cenLat, $cenLng, $lat, $lng) {
    $radiusInKm = intval($radiusInKm);
    $cenLat = floatval($cenLat);
    $cenLng = floatval($cenLng);
    $lat = floatval($lat);
    $lng = floatval($lng);
    $distance = ( 6371 * acos( cos( deg2rad($cenLat) ) * cos( deg2rad( $lat ) ) * cos( deg2rad( $lng ) - deg2rad($cenLng) ) + sin( deg2rad($cenLat) ) * sin( deg2rad( $lat ) ) ) );
    if($distance <= $radiusInKm){
        return true;
    } else {
        return false;
    }
}

Solution

  • If the radius is less than 1, it becomes 0, and this returns the results of a zero size circle:

    function isPointInRadius($radiusInKm, $cenLat, $cenLng, $lat, $lng) {
      $radiusInKm = intval($radiusInKm);
    

    There won't be any results in a zero size circle. Either change it from intval to floatval or change the units to meters (< 1 meter probably will still return zero results, but that is to be expected).