Friday 5 September 2014

Marker Clusterer for Google MAP API


CSS :
<style>
        html, body, form {
            margin: 0;
            font-family: Arial;
            font-size: 16px;
            height: 100%;
            width: 100%;
        }


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


Add google MAp API link here and also Markerdata.JS

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script src="script/data.js"></script>
<script src="script/markerdata.js"></script>


Write Below JS code in your Page

var markers = [];
        var markerCluster
        function initialize() {
            var center = new google.maps.LatLng(37.4419, -122.1419);

            var map = new google.maps.Map(document.getElementById('map'), {
                zoom: 3,
                center: center,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });


            for (var i = 0; i < data.length; i++) {
                var latLng = new google.maps.LatLng(data[i].latitude, data[i].longitude);
                var marker = new google.maps.Marker({
                    position: latLng
                });
                markers.push(marker);
            }
            markerCluster = new MarkerClusterer(map, markers, { gridSize: 20 });
        }
        google.maps.event.addDomListener(window, 'load', initialize);


        function RemoveMarker() {
            var temp = '1'; // Will remove first marker from Array
            if (markerCluster != undefined) {
                if (markerCluster.removeMarker(markers[parseInt(temp.value)]))
                    markers.splice(parseInt(temp.value), 1);

            }

        }

        function AddMarker() {
            var latLng = new google.maps.LatLng(37.4419, -122.1419);
            var marker = new google.maps.Marker({
                position: latLng
            });
            if (markerCluster.addMarker(marker))
                markers.push(marker)
        }



Add Below HTML

  <div id="map"></div>
   <div>
            <input type="text" id="txt" />
            <input type="button" value="remove" id="btn" onclick="RemoveMarker();" />

   </div>




Download sample from Here
Output will like below.


No comments:

Post a Comment