wecuro_blog/application/helpers/map_helper.20210128.php

52 lines
1.5 KiB
PHP
Executable File

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if (!function_exists('map_address')){
function map_address($address,$api=''){
// $apiKey = 'AIzaSyAlmhyud5rrzHErOCq8FLV11NhfBFQpYlE'; // DEV API
// $apiKey = 'AIzaSyAlmhyud5rrzHErOCq8FLV11NhfBFQpYlE'; // client Google maps now requires an API key.
// SGeek API.
if ($api=='')
{
$apiKey = 'AIzaSyATGiMKU2Gt2-mdQA20xdPm4Of5WsomO1s';
}
else
{
$apiKey = $api;
}
$prepAddr = str_replace(' ','+',$address);
$url = "https://maps.google.com/maps/api/geocode/json?address=".urlencode($address).'&sensor=false&key='.$apiKey;
// https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=AIzaSyC8vrcmnC6B1iLmvFofk3DAy4SGVOhjRQQ
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$responseJson = curl_exec($ch);
curl_close($ch);
$response = json_decode($responseJson);
$resp = array();
if ($response->status == 'OK') {
$latitude = $response->results[0]->geometry->location->lat;
$longitude = $response->results[0]->geometry->location->lng;
$resp = array(
"status" => "success",
"latlong" => array("Latitude" => $latitude , "Longitude" => $longitude),
);
} else {
$resp = array(
"api" => $apiKey,
"status" => $response->status,
"error" => $response
);
}
return $resp;
}
}
?>