map show by address using php jquery

<?php
$address = $row['business_address'];
// $url = "http://maps.google.com/maps/api/geocode/json?address=".urlencode($address);
$url = "https://maps.googleapis.com/maps/api/geocode/json?address='".urlencode($address)."'&sensor=true&key=AIzaSyCBK8biD4eeBl4E1GoRdh57ndMTHWfYQX4";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   
$responseJson = curl_exec($ch);
curl_close($ch);

$response = json_decode($responseJson);

if ($response->status == 'OK') {
    $latitude = $response->results[0]->geometry->location->lat;
    $longitude = $response->results[0]->geometry->location->lng;
   /* echo 'Latitude: ' . $latitude;
    echo '<br />';
    echo 'Longitude: ' . $longitude;*/
} else {
    echo $response->status;
    var_dump($response);
}   
?>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCBK8biD4eeBl4E1GoRdh57ndMTHWfYQX4"></script>
<script type="text/javascript">
var markers = [
    {
        "title": '<?php echo $row['business_email']; ?>',
        "lat": '<?php echo $latitude; ?>',
        "lng": '<?php echo $longitude; ?>',
        "description": '<?php echo $address; ?>'
    }
    ];

    window.onload = function () {
        LoadMap();
    }
    function LoadMap() {
        var mapOptions = {
            center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);

        //Create and open InfoWindow.
        var infoWindow = new google.maps.InfoWindow();

        for (var i = 0; i < markers.length; i++) {
            var data = markers[i];
            var myLatlng = new google.maps.LatLng(data.lat, data.lng);
            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: data.title
            });

            //Attach click event to the marker.
            (function (marker, data) {
                google.maps.event.addListener(marker, "click", function (e) {
                    //Wrap the content inside an HTML DIV in order to set height and width of InfoWindow.
                    infoWindow.setContent("<div style = 'width:200px;min-height:40px'>" + data.description + "</div>");
                    infoWindow.open(map, marker);
                });
            })(marker, data);
        }
    }
</script>
    <style type="text/css">
#mapContainer {
    height: 200px;
}

#dvMap {
    width: 100%;
    height: 100%;
}
</style>  

Comments

Popular posts from this blog

dynamic create multiple ckeditor onclick event using jquery

get lat and long from city name using jquery

membership registration and login system in php