Tuesday, November 22, 2016

Getting Full detail of any particular location using Geocoding in PHP


///////////////////////////////////////////////////////////////////////////////////////////////////
//// index.php
///////////////////////////////////////////////////////////////////////////////////////////////////
<form  method="post" action="geoclass.php">
<h2 align="center" style="vertical-align:middle">Geocode This Address</h2>
<table align="center" style="vertical-align:middle">
<tr>
<td>Appt No:</td><td><input type="text" id="appt" name="appt"></td></tr>
<td>Street Address:</td><td><input type="text" id="street" name="street"></td></tr>
<td>City:</td><td><input type="text" id="city" name="city"></td></tr>
<td>State:</td><td><input type="text" id="state" name="state"></td></tr>
<td>Zip Code:</td><td><input type="text" id="zip" name="zip"></td></tr>
<td>Country:</td><td><input type="text" id="country" name="country"></td></tr>
<td></td><td><input type="submit" id="sub" name="sub" value="Enter The Address Here!!!"></td>
</tr>
</table>
</form>
///////////////////////////////////////////////////////////////////////////////////////////////////
//// End index.php
///////////////////////////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////////////////////////
//// geoclass.php //using curl
///////////////////////////////////////////////////////////////////////////////////////////////////
<?php

class geocoder{
   static private $url="http://maps.google.com/maps/api/geocode/xml?sensor=true&address=";
   static public function getLocation($address){
       $url=self::$url.urlencode($address);
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$data1=curl_exec($ch);
curl_close($ch);
$data2=simplexml_load_string($data1);
$data2->asXML("result.xml");
$arr=array();
if($data2->status=='OK'){

$arr[]=(string)$data2->result->geometry->location->lat;
$arr[]=(string)$data2->result->geometry->location->lng;
return $arr;
}
   }


}//end class
if(!isset($_REQUEST['sub'])){
$appt=$_REQUEST['appt'];
$street=$_REQUEST['street'];
$city=$_REQUEST['city'];
$state=$_REQUEST['state'];
$zip=$_REQUEST['zip'];
$country=$_REQUEST['country'];
$myaddress=$appt." ".$street." ".$city." ".$state." ".$zip." ".$country;
$address=urlencode($myaddress);
$loc = geocoder::getLocation($address);
}
else{
$address=urlencode("1600 Amphitheatre Parkway, Mountain View, CA");
//$address=urlencode($myaddress);
$loc = geocoder::getLocation($address);
}
?>

<script src="http://maps.googleapis.com/maps/api/js?key=API_KEY"></script>
<script>


var myLatlng = new google.maps.LatLng('<?php echo $loc[0]; ?>','<?php echo $loc[1]; ?>');


function initialize()
{
var mymapProp = {
  center: myLatlng,
  zoom:5,
  mapTypeId: google.maps.MapTypeId.ROADMAP
  };

var map = new google.maps.Map(document.getElementById("displayarea"),mymapProp);

var image = 'images/myimage.png';
image.height = 5;
image.width = 5;
var marker = new google.maps.Marker({
  position: myLatlng,
   map: map,
   icon: image,
   title:'Click for zooming muna map'
  });

marker.setMap(map);
google.maps.event.addListener(marker,'click',function() {
  map.setZoom(12);//// Zoom to 9 when clicking on marker
  map.setCenter(marker.getPosition());
  });
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<div  align="center" id="displayarea" style="width:500px;height:380px;" ></div>


///////////////////////////////////////////////////////////////////////////////////////////////////
//// End geoclass.php
///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////
//// geoclass.php //using file_get_contents
///////////////////////////////////////////////////////////////////////////////////////////////////
<?php

class geocoder{
    static private $url = "http://maps.google.com/maps/api/geocode/xml?sensor=false&address=";
    static public function getLocation($address){
        $url = self::$url.urlencode($address);
$resp_json =file_get_contents($url);
   $data=simplexml_load_string($resp_json);
$data->asXML("googleres.xml");
$arr=array();
if($data->status=='OK'){
$arr[]=(string)$data->result->geometry->location->lat;
$arr[]=(string)$data->result->geometry->location->lng;
return $arr;
}



$resp = json_decode($resp_json, true);
if($resp['status']=='OK'){
            return $resp['results'][0]['geometry']['location'];
        }else{
            return false;
        }  
    }
}
if(isset($_REQUEST['sub'])){
$appt=$_REQUEST['appt'];
$street=$_REQUEST['street'];
$city=$_REQUEST['city'];
$state=$_REQUEST['state'];
$zip=$_REQUEST['zip'];
$country=$_REQUEST['country'];
$myaddress=$appt." ".$street." ".$city." ".$state." ".$zip." ".$country;
$address=urlencode($myaddress);
$loc = geocoder::getLocation($address);
}
else{
$address=urlencode("1600 Amphitheatre Parkway, Mountain View, CA");
$address=urlencode($myaddress);
$loc = geocoder::getLocation($address);
}
?>

<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDfdjaFVpr7tz2oA-FQ0_e4WJM8zN9StCI"></script>
<script>


var myLatlng = new google.maps.LatLng('<?php echo $loc[0]; ?>','<?php echo $loc[1]; ?>');


function initialize()
{
var mymapProp = {
  center: myLatlng,
  zoom:5,
  mapTypeId: google.maps.MapTypeId.ROADMAP
  };

var map = new google.maps.Map(document.getElementById("displayarea"),mymapProp);

var image = 'images/myimage.png';
image.height = 5;
image.width = 5;
var marker = new google.maps.Marker({
  position: myLatlng,
   map: map,
   icon: image,
   title:'Click for zooming muna map'
  });

marker.setMap(map);
google.maps.event.addListener(marker,'click',function() {
  map.setZoom(12);//// Zoom to 9 when clicking on marker
  map.setCenter(marker.getPosition());
  });
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<div  align="center" id="displayarea" style="width:500px;height:380px;" ></div>


///////////////////////////////////////////////////////////////////////////////////////////////////
//// End geoclass.php
///////////////////////////////////////////////////////////////////////////////////////////////////







///////////////////////////////////////////////////////////////////////////////////////////////////
//// result.xml
///////////////////////////////////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
 <status>OK</status>
 <result>
  <type>street_address</type>
  <formatted_address>1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA</formatted_address>
  <address_component>
   <long_name>1600</long_name>
   <short_name>1600</short_name>
   <type>street_number</type>
  </address_component>
  <address_component>
   <long_name>Amphitheatre Parkway</long_name>
   <short_name>Amphitheatre Pkwy</short_name>
   <type>route</type>
  </address_component>
  <address_component>
   <long_name>Mountain View</long_name>
   <short_name>Mountain View</short_name>
   <type>locality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Santa Clara County</long_name>
   <short_name>Santa Clara County</short_name>
   <type>administrative_area_level_2</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>California</long_name>
   <short_name>CA</short_name>
   <type>administrative_area_level_1</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>United States</long_name>
   <short_name>US</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>94043</long_name>
   <short_name>94043</short_name>
   <type>postal_code</type>
  </address_component>
  <geometry>
   <location>
    <lat>37.4223664</lat>
    <lng>-122.0844060</lng>
   </location>
   <location_type>ROOFTOP</location_type>
   <viewport>
    <southwest>
     <lat>37.4210174</lat>
     <lng>-122.0857550</lng>
    </southwest>
    <northeast>
     <lat>37.4237154</lat>
     <lng>-122.0830570</lng>
    </northeast>
   </viewport>
  </geometry>
  <partial_match>true</partial_match>
  <place_id>ChIJ2eUgeAK6j4ARbn5u_wAGqWA</place_id>
 </result>
</GeocodeResponse>

///////////////////////////////////////////////////////////////////////////////////////////////////
//// End result.php
///////////////////////////////////////////////////////////////////////////////////////////////////


No comments:

Post a Comment