Select your language
To calculate the distance in meters between your current location and a property retrieved from a website using Google APIs in a Joomla environment, you can use the Google Maps Distance Matrix API. Here's a step-by-step guide:
1. Set up a Google API key
2. Integrate the API in Joomla
Function to get distance, can be places inside controller method, once it is called using AJAX, controller method will fetch distance in meter(s):
<?php public function get_distance(){ //if latitude and longitude are submitted $latitude=JRequest::getVar('latitude'); $longitude=JRequest::getVar('longitude'); $item_latitude=JRequest::getVar('item_latitude'); $item_longitude=JRequest::getVar('item_longitude'); if(!empty($latitude) && !empty($longitude)){ //send request and receive json data by latitude and longitude $url = 'https://maps.googleapis.com/maps/api/geocode/json?key=YOURKEYHERE&latlng='.trim($latitude).','.trim($longitude).'&sensor=false'; $json = @file_get_contents($url); $data = json_decode($json); $status = $data->status; //if request status is successful if($status == "OK"){ //get address from json data $location = $data->results[0]->formatted_address; }else{ $location = ''; } //return address to ajax /* .................. GET KM -----------------------*/ // Addresses between which distances will be calculated $addressFrom = $location; $addressTo = JRequest::getVar('addressTo'); // Format address string $formatted_address_from = str_replace(array(' ','&'), '+', $addressFrom); $formatted_address_to = str_replace(array(' ','&'), '+', $addressTo); // Geocoding API request with start address $a_url="https://maps.googleapis.com/maps/api/distancematrix/json?destinations=".urlencode($addressFrom)."&origins=".urlencode($addressTo)."&units=imperial&key=AIzaSyDSS70hvcoWwGoJWnYfsNM8eOTzdybk-rg"; $geocode_data_start = file_get_contents($a_url); $outputFrom = json_decode($geocode_data_start); $distance_mile=(int)$outputFrom->rows[0]->elements[0]->distance->text; // Get the distance in meters $distance_m=$outputFrom->rows[0]->elements[0]->distance->value; // $distance_km=round(($distance_mile * 1.609344), 2); // Convert meters to kilometers and format to two decimal places $kilometers = number_format($distance_m / 1000, 2); echo "$kilometers Km";exit; }
Place following code inside view template:
<span class="location_distance" id="location_distance<?=$item->id?>"></span> <!-- SHOW LOCATION AND CALCULATE DISTANCE between two lati, long(location) --> <style> .location_distance { color:yellow; font-weight: bold; font-style: italic; text-decoration: underline; } </style> <script> jQuery(document).ready(function(){ if(navigator.geolocation){ navigator.geolocation.getCurrentPosition(showLocation<?=$item->id?>); }else{ alert('Geolocation is not supported by this browser.'); } }); function showLocation<?=$item->id?>(position){ var latitude = position.coords.latitude; var longitude = position.coords.longitude; jQuery.ajax({ type:'POST', url:'index.php?option=com_bt_property&task=properties.get_distance&latitude='+latitude+'&longitude='+longitude+'&item_latitude=<?=$item->latitude?>&item_longitude=<?=$item->latitude?>', data:'latitude='+latitude+'&longitude='+longitude+'&item_latitude=<?=$item->latitude?>&item_longitude=<?=$item->latitude?>&addressTo=<?=$item->address?>', success:function(msg){ // alert(msg); if(msg){ jQuery("#location_distance<?=$item->id?>").html(msg); }else{ jQuery("#location_distance<?=$item->id?>").html('Not Available'); } } }); } </script>
Hope this helped.
Still need help! no problem, feel free to contact us Today
Abdul Waheed : (Hire Joomla & PHP Pakistani Freelancer)