public function validate_address(Request $request)
{
$this->validate($request, [
'address' => 'required',
]);
try {
$address = urlencode($request->address);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"https://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&key=" . config('homepilot.map_key'));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$place=curl_exec($ch);
curl_close($ch);
$place = json_decode($place);
if ($place->status != 'OK') {
throw new Exception('User input wrong address: ' . $address);
}
$address_obj = [];
$address_obj['address'] = $place->results[0]->formatted_address;
$address_obj['lat'] = $place->results[0]->geometry->location->lat;
$address_obj['lng'] = $place->results[0]->geometry->location->lng;
foreach ($place->results[0]->address_components as $address_component) {
switch ($address_component->types[0]) {
case 'street_number':
$address_obj['street_number'] = $address_component->short_name;
break;
case 'route':
$address_obj['street_route'] = $address_component->long_name;
break;
case 'locality':
$address_obj['city'] = $address_component->long_name;
break;
case 'administrative_area_level_1':
$address_obj['state'] = $address_component->short_name;
break;
case 'postal_code':
$address_obj['postal_code'] = $address_component->short_name;
break;
case 'country':
$address_obj['country'] = $address_component->long_name;
break;
}
}
$address_step = [
'data' => $address_obj,
'form_data' => null,
'active' => true,
'type' => 'user_address'
];
return $address_step;
} catch (\Exception $exception) {
return [
'success' => false,
'html' => \View::make('frontend._no-service-modal')->render()
];
}
}
Created
August 21, 2021 10:16
-
-
Save huzaifa-a/a90d80be158dfbcd44c42a94444538cd to your computer and use it in GitHub Desktop.
get coordinates from address
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment