218 lines
8.0 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.googleapis.com/maps/api/place/details/json?place_id=ChIJN1t_tDeuEmsRUsoyG83frY4&fields=google hq&key=".$apiKey;
// $url = "https://maps.google.com/maps/api/geocode/json?address=".urlencode($address).'&sensor=false&key='.$apiKey;
// // $url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=40.6905615%2C-73.9976592%7C40.6905615&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
// );
// }
// $city = '';
// $county = '';
// $state = '';
// $stateSmall = '';
// $lat = '';
// $long = '';
// $address_components = $response->results[0]->address_components;
// $formatted_address = $response->results[0]->formatted_address;
// $geometry = $response->results[0]->geometry;
// $lat = $geometry->location->lat;
// $long = $geometry->location->lng;
// foreach ($address_components as $value) {
// if(in_array('administrative_area_level_2', $value->types))
// {
// $county = $value->long_name;
// }
// if(in_array('locality', $value->types))
// {
// $city = $value->long_name;
// }
// if(in_array('administrative_area_level_1', $value->types))
// {
// $state = $value->long_name;
// $stateSmall = $value->short_name;
// }
// if($city==''){
// $city = $state;
// }
// }
// // return $response;
// return array('city' => $city, 'county' => $county, 'state' => $state, 'stateShort' => $stateSmall, 'lat' => $lat, 'long' => $long);
// }
// }
if (!function_exists('mapquest_address')){
function mapquest_address($address,$zipcode)
{
$CI = &get_instance();
$CI->load->model('home/Home_model');
$apiKey = MAPQUEST_API_KEY;
$prepAddr = $address.','.$zipcode;
$url = "https://www.mapquestapi.com/geocoding/v1/address?key=".$apiKey."&location=".urlencode($prepAddr)."";
// return $url;
$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);
if(isset($response->results[0]->locations[0]->adminArea5))
$city=$response->results[0]->locations[0]->adminArea5;
if(isset($response->results[0]->locations[0]->adminArea4))
$county=$response->results[0]->locations[0]->adminArea4;
if(isset($response->results[0]->locations[0]->adminArea3)){
$state_code=$response->results[0]->locations[0]->adminArea3;
$state_arr=$CI->Home_model->name_by_code($state_code);
$state=$state_arr->name;
}
if(isset($response->results[0]->locations[0]->latLng->lat))
$latitude=$response->results[0]->locations[0]->latLng->lat;
if(isset($response->results[0]->locations[0]->latLng->lng))
$longitude=$response->results[0]->locations[0]->latLng->lng;
return array('city' => $city, 'county' => $county, 'state' => $state, 'stateShort' => $state_code, 'lat' => $latitude, 'long' => $longitude);
}
}
if (!function_exists('distanceApi')){
function distanceApi($frmLat="40.6655101",$frmLon="-73.89188969999998",$toLat="40.6905615",$toLon="-73.9976592",$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.googleapis.com/maps/api/place/details/json?place_id=ChIJN1t_tDeuEmsRUsoyG83frY4&fields=google hq&key=".$apiKey;
// $url = "https://maps.google.com/maps/api/geocode/json?address=".urlencode($address).'&sensor=false&key='.$apiKey;
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=$frmLat,$frmLon&destinations=$toLat,$toLon&key=$apiKey";
// echo $url; die;
// 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
);
}
// _die($response);
$destination_addresses = $response->destination_addresses[0];
$origin_addresses = $response->origin_addresses[0];
$distance = $response->rows[0]->elements[0]->distance->text;
$duration = $response->rows[0]->elements[0]->duration->text;
return array('destination_addresses'=>$destination_addresses,'origin_addresses'=>$origin_addresses,'distance'=>$distance,'duration' => $duration);
}
}
if (!function_exists('nearestCity')){
function nearestCity($lat='15.495602',$long='73.825209',$api='')
{
if ($api=='')
{
$apiKey = 'AIzaSyATGiMKU2Gt2-mdQA20xdPm4Of5WsomO1s';
}
// $url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={$lat},{$long}&radius=1500&types=hotel&sensor=false&key={$apiKey}";
$url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={$lat},{$long}&radius=300000&types=post_office&key={$apiKey}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
$dataset = $response_a->results;
$city = array();
foreach ($dataset as $data) {
// echo $data->results[0]->vicinity;
$a = explode(',', $data->vicinity);
if(!in_array($a[1], $city))
{
$city[] = $a[1];
}
}
return $city;
// echo $name = $response_a->results[0]->name;
// echo "<br />";
// echo $vicinity = $response_a->results[0]->vicinity;
}
}
?>