1var x=document.getElementById("demo");
2function getLocation(){
3 if (navigator.geolocation){
4 navigator.geolocation.getCurrentPosition(showPosition,showError);
5 }
6 else{
7 x.innerHTML="Geolocation is not supported by this browser.";
8 }
9}
10
11function showPosition(position){
12 lat=position.coords.latitude;
13 lon=position.coords.longitude;
14 displayLocation(lat,lon);
15}
16
17function showError(error){
18 switch(error.code){
19 case error.PERMISSION_DENIED:
20 x.innerHTML="User denied the request for Geolocation."
21 break;
22 case error.POSITION_UNAVAILABLE:
23 x.innerHTML="Location information is unavailable."
24 break;
25 case error.TIMEOUT:
26 x.innerHTML="The request to get user location timed out."
27 break;
28 case error.UNKNOWN_ERROR:
29 x.innerHTML="An unknown error occurred."
30 break;
31 }
32}
33
34function displayLocation(latitude,longitude){
35 var geocoder;
36 geocoder = new google.maps.Geocoder();
37 var latlng = new google.maps.LatLng(latitude, longitude);
38
39 geocoder.geocode(
40 {'latLng': latlng},
41 function(results, status) {
42 if (status == google.maps.GeocoderStatus.OK) {
43 if (results[0]) {
44 var add= results[0].formatted_address ;
45 var value=add.split(",");
46
47 count=value.length;
48 country=value[count-1];
49 state=value[count-2];
50 city=value[count-3];
51 x.innerHTML = "city name is: " + city;
52 }
53 else {
54 x.innerHTML = "address not found";
55 }
56 }
57 else {
58 x.innerHTML = "Geocoder failed due to: " + status;
59 }
60 }
61 );
62}
1function geoLocate($address)
2{
3 try {
4 $lat = 0;
5 $lng = 0;
6
7 $data_location = "https://maps.google.com/maps/api/geocode/json?key=".$GOOGLE_API_KEY_HERE."&address=".str_replace(" ", "+", $address)."&sensor=false";
8 $data = file_get_contents($data_location);
9 usleep(200000);
10 // turn this on to see if we are being blocked
11 // echo $data;
12 $data = json_decode($data);
13 if ($data->status=="OK") {
14 $lat = $data->results[0]->geometry->location->lat;
15 $lng = $data->results[0]->geometry->location->lng;
16
17 if($lat && $lng) {
18 return array(
19 'status' => true,
20 'lat' => $lat,
21 'long' => $lng,
22 'google_place_id' => $data->results[0]->place_id
23 );
24 }
25 }
26 if($data->status == 'OVER_QUERY_LIMIT') {
27 return array(
28 'status' => false,
29 'message' => 'Google Amp API OVER_QUERY_LIMIT, Please update your google map api key or try tomorrow'
30 );
31 }
32
33 } catch (Exception $e) {
34
35 }
36
37 return array('lat' => null, 'long' => null, 'status' => false);
38}
39
1
2
3<!DOCTYPE html>
4
5<html>
6
7<form method="post">
8
9<input type="text" name="address">
10
11<input type="submit" name="submit" value="submit">
12
13</form>
14
15</html>
16
17<?php
18
19if(isset($_POST['submit']))
20
21{
22
23
24function getLatLong($address){
25
26 if(!empty($address)){
27
28 //Formatted address
29
30 $formattedAddr = str_replace(' ','+',$address);
31
32 //Send request and receive json data by address
33
34 $geocodeFromAddr = file_get_contents
35('http://maps.googleapis.com/maps/api/geocode/json?address='.$formattedAddr.'&sensor=false');
36
37 $output = json_decode($geocodeFromAddr);
38
39 //Get latitude and longitute from json data
40
41 $data['latitude'] = $output->results[0]->geometry->location->lat;
42
43 $data['longitude'] = $output->results[0]->geometry->location->lng;
44
45 //Return latitude and longitude of the given address
46
47 if(!empty($data)){
48
49 return $data;
50
51 }else{
52
53 return false;
54
55 }
56
57 }else{
58
59 return false;
60
61 }
62
63}
64
65$address = $_POST['address'];
66
67$latLong = getLatLong($address);
68
69$latitude = $latLong['latitude']?$latLong['latitude']:'Not found';
70
71$longitude = $latLong['longitude']?$latLong['longitude']:'Not found';
72
73echo "Latitude:".$latitude."<br>";
74
75echo "longitude:".$longitude."";
76
77}
78
79?>
80