Custom Web Applications to Grow Your Business

(256) 429-8145

PHP Google Map API Class with physical address as data input

[caption id="attachment_505" align="alignleft" width="651"]google place. local huntsville al business listing Get Your Local Business on Google Places[/caption]If you ever wanted to create a simple php class to render Google maps. Here is one that you can use

This section must be in the head of the page

// begin class class GoogleMapAPI { var $api_key = ''; var $latt = ''; var $longg = ''; // function with api key. If you do not have an api key // then you will have to get a Google API key // https://developers.google.com/maps/documentation/javascript/reference function setAPIKey($key) { $this->api_key = $key; } // include the Maps API JavaScript using a script tag function getHeaderJS() { return sprintf('<script src="https://maps.googleapis.com/maps/api/js?key=%s&sensor=false" type="text/javascript" charset="utf-8"></script>', $this->api_key); } function printHeaderJS() { echo $this->getHeaderJS(); } // lat and long through json function addMarkerByAddress($address,$city,$state, $zip) { $fullAddress = $address.','.$city.','.$state.','.$zip; $fullAddress = str_replace(' ', '+', $fullAddress); $fullurl = "http://maps.googleapis.com/maps/api/geocode/json?address=".$fullAddress."&sensor=false"; $string .= file_get_contents($fullurl); // get json content $json_a = json_decode($string, true); //json decoder $this->latt = $json_a['results'][0]['geometry']['location']['lat']; // get lat for json $this->longg = $json_a['results'][0]['geometry']['location']['lng']; // get ing for json } function getMapJS() { $_output = '<script type="text/javascript">'; $_output .= 'var map;'; $_output .=' var panorama;'; $_output .= 'var astorPlace = new google.maps.LatLng('.$this->latt.', '.$this->longg.');'; $_output .= 'function initialize() {'; // Set up the map $_output .= 'var mapOptions = {'; $_output .= 'center: astorPlace,'; $_output .= 'zoom: 16,'; $_output .= 'mapTypeId: google.maps.MapTypeId.ROADMAP,'; $_output .= 'streetViewControl: false'; $_output .= '};'; $_output .= "map = new google.maps.Map(document.getElementById('map_canvas'),"; $_output .= 'mapOptions); '; // We get the map's default panorama and set up some defaults. // Note that we don't yet set it visible. $_output .= 'panorama = map.getStreetView();'; $_output .= 'panorama.setPosition(astorPlace);'; $_output .= 'panorama.setPov({'; $_output .= ' heading: 265,'; $_output .= ' zoom:1,'; $_output .= ' pitch:0}'; $_output .= ' );'; $_output .= ' }'; $_output .= ' function toggleStreetView() {'; $_output .= ' var toggle = panorama.getVisible();'; $_output .= 'if (toggle == false) {'; $_output .= ' panorama.setVisible(true);'; $_output .= '} else {'; $_output .= 'panorama.setVisible(false);'; $_output .= '}'; $_output .= ' }'; $_output .= ' </script>'; return $_output; } }

This section in the body of the page

<body onload="initialize()"> <div id="toggle"> <input type="button" value="Toggle Street View" onclick="toggleStreetView();"></input> </div> <div id="map_canvas" style="position:relative;width: 500px; height: 350px"></div> </body> </html>