Use Google Map without key for dynamic address
<?php
$address = "Mavviya Nagar Jaipur";
$address = str_replace(" ", "+", $address);
$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false");
$json = json_decode($json);
$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
$aLocation = array("title" => 'title',"content" => 'Content',"lat" => $lat,"lon" => $long);
$aLocation = json_encode($aLocation);
?>
<div id="map" style="width:500px;height:400px;"></div>
<script>
var aLocation = <?php echo $aLocation; ?>;
function initMap() {
myLatLng = {lat: aLocation['lat'], lng: aLocation['lon']};
var title = aLocation['title'];
var content = aLocation['content'];
map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
scrollwheel: false,
center: myLatLng
});
var infowindow = new google.maps.InfoWindow()
var marker = new google.maps.Marker({
map: map, title: title , position: myLatLng
});
google.maps.event.addListener(marker,'click', (function(marker,content,infowindow){
return function() {
infowindow.setContent(content);
infowindow.open(map,marker);
};
})(marker,content,infowindow));
}
</script>
<script async defer src="https://maps.google.com/maps/api/js?sensor=false&callback=initMap"></script>
//to handle error if not result found
<?php
$address = $product_address;
$address = str_replace(" ", "+", $address);
$json = @file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false");
$json = @json_decode($json);
if($json->{'status'}!="ZERO_RESULTS"){
$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
$aLocation = array("title" => 'Location',"content" => $product_address,"lat" => $lat,"lon" => $long);
$aLocation = json_encode($aLocation);
?>
<div class="map">
<div id="map" style="width:360px;height:300px;"></div>
</div>
<?php } else{ } ?>
Comments
Post a Comment