Working with Google Map & Markers API v.2

<!--HTML -->
<div id="map"></div>
//JavaScript
<script src="https://maps.googleapis.com/maps/api/js?key=your_key"></script>
<script src="~/Areas/Admin/Assets2/markerclusterer.js"></script>

//Draw a google map without any marker on page load 
<script>
    var map;
    var markers = [];
    $(function () {
        var mapOptions = {
            center: { lat: 23.564587, lng: 90.569874 },
            zoom: 7,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
            map = new google.maps.Map(document.getElementById("map"), mapOptions);
    });
</script>


//--> Load marker data on search button click event 
<script>
    var $SubmitSearch = $('#SubmitSearch');
      
    //-->document.ready()
    $(function () {
        $SubmitSearch.click(function (e) {
            e.preventDefault();
            var fd = new FormData();

            $.ajax({
                type: 'POST',
                url: "../../../../Admin/Search/SearchResult/",
                processData: false,  // tell jQuery not to process the data
                contentType: false,   // tell jQuery not to set contentType
                data: fd,
                beforeSend: function () {
                    $SubmitSearch.attr('disabled', 'disabled');
                    $SubmitSearch.html('Working…');
                },
                success: function (response) {
                    deleteMarkers();
                    var hasData = response.hasdata;
                    if (hasData) {
                        var sites = JSON.parse(response.data);
                        var serialNo = 0;
                        //-->Looping the parsed data
                        sites.forEach(function (site, index) {
                            serialNo++;

                            var marker = new google.maps.Marker({
                                name: "12345",
                                position: { lat: site.Latitude, lng: site.Longitude },
                                map: map,
                                //icon: "../../Assets/images/google-map-markers/OutletMarker.png",
                                title: site.SiteId.toString()
                            });

                            marker.addListener('click', function () {

                                var contentString = '';

                                //-->Fetch default images from server
                                $.ajax({
                                    type: "POST",
                                    url: '../../../Common/GetMarkerDetails',
                                    contentType: "application/json; charset=utf-8",
                                    data: "{'siteId':'" + site.SiteId + "'}",
                                    dataType: "json",
                                    beforeSend: function () {

                                    },
                                    success: function (result) {
                                        contentString = '<img src="http://localhost:63716/Images/'+ result.ImageName +'" />';
                                    },
                                    error: function (request, status, error) {

                                    }
                                });
                                //<--- Fetch default images from server

                                var infowindow = new google.maps.InfoWindow({
                                    content: contentString
                                });

                                infowindow.open(map, marker);
                            });

                            markers.push(marker);
                        });
                        //<--Looping the parsed data


                        //-->Add marker clustering
                        var markerCluster = new MarkerClusterer(map, markers, {
                            gridSize: 40,
                            minimumClusterSize: 50
                        });
                        //-->Add marker clustering

                    }
                    else {
                        alert('Your search did not match with any record.')
                        $ShowMore.hide(); // No need to show anymore.
                        $('#resultCount').hide();
                    }

                },
                error: function (request, status, error) {
                    alert('Unknown error.');
                    $ShowMore.html('Error in loading data');
                    $ShowMore.attr('disabled', 'disabled');
                },
                complete: function () {
                    // ShowMarketResearchPlanningPopup();
                    $SubmitSearch.removeAttr('disabled');
                    $SubmitSearch.html('Search');
                }
            });

        });
        //<-- Submit Search

        // Sets the map on all markers in the array.
        function setMapOnAll(map) {
            for (var i = 0; i < markers.length; i++) {
                markers[i].setMap(map);
            }
        }

        // Removes the markers from the map, but keeps them in the array.
        function clearMarkers() {
            setMapOnAll(null);
        }

        // Shows any markers currently in the array.
        function showMarkers() {
            setMapOnAll(map);
        }

        // Deletes all markers in the array by removing references to them.
        function deleteMarkers() {
            clearMarkers();
            markers = [];
        }

    });
    //<-- document.ready()
</script>
//<-- Load marker data on search button click event

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s