Sunday 5 January 2014

Google Map API Zoom The Marker Example

Leave a Comment
Google Map API Marker is a powerful API to use for web page to mark on the map for example the location of your business office. This tutorial can use in all framework as long as the framework use html language to render the content to client side.

You can refer here for more information of Google Map API

Let see the example in asp.net framework.

The Javascript to call map and marker

var map;
function initializeGMap() {
           var Centre = new google.maps.LatLng(3.15681, 101.714696);
            var mapOptions = { zoom: 12, center: Centre, mapTypeId: google.maps.MapTypeId.ROADMAP };
            map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

            var KLCC = new google.maps.LatLng(3.15681, 101.714696);
            var marker = new google.maps.Marker({ position: KLCC, map: map, title: 'Kuala Lumpur City Center' });  
        }
        google.maps.event.addDomListener(window, 'load', initializeGMap);


The Zoom Marker Script

function getInfo() {
            var contentString = '<div style="width: 400px; min-height: 100px;">';
            contentString += '<table style="font-size:12px; width:400px; height: 100px; border: none !important;">';
            contentString += '<tr style=\"background:none !important;\">';
            contentString += '<td valign=\"top\" style=\"width: 80px; height: 80px;border-bottom:none;\"></td>';
            contentString += '<td valign=\"top\" style=\"border-bottom:none;\">';
            contentString += '<table><tr style=\"background:none !important;\">';
            contentString += '<td style=\"font-size:16px;border-bottom:none;\">';
            contentString += '<b>Kuala Lumpur City Centre</b></td></tr>';
            contentString += '<tr style=\"background:none !important;\">';
            contentString += '<td style=\"border-bottom:none;\">';
            contentString += 'Kuala Lumpur City Centre<br />50450 Kuala Lumpur<br/>Wilayah Persekutuan Kuala Lumpur, Malaysia</td>';
            contentString += '</tr></table></td></tr><tr style=\"background:none !important;\">';
            contentString += '<td style=\"border-bottom:none;\">&nbsp;</td></tr>';
            contentString += '<tr style=\"background:none !important;border-bottom:none;\">';
            contentString += '<td colspan="2"></td></tr><tr style="background:none !important;">';
            contentString += '<td colspan="2" style=\"border-bottom:none;\"><b>Phone:</b>-</td>';
            contentString += '</tr></td></tr></table></div>';
            var infoWindowMarker = new google.maps.InfoWindow({
                content: contentString
            });
            var myLatLng = new google.maps.LatLng(3.15681, 101.714696);
            var myMarker = new google.maps.Marker({
                position: myLatLng,
                map: map,
                title: 'Kuala Lumpur City Centre'
            });
            infoWindowMarker.close();
            infoWindowMarker.open(map, myMarker);
            map.setCenter(myLatLng);
            map.setZoom(17);

        }


The ASPX Page

<div style="width: 600px; height: 400px; float:left;">
        <div id="map-canvas" style="width: 100%; height: 100%;">
        </div>
    </div>
    <div style="float:right;">
        <div id="Div1" style="width: 100%; height: 100%; text-align:left; padding:20px;">
            <asp:Button ID="Button1" runat="server" Text="Zoom The Marker" OnClientClick="getInfo();return false;" />
        </div>
    </div>


Full Code

Note : This example use default MasterPage from Visual Studio 2010

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>

        var map;

        function initializeGMap() {

            var Centre = new google.maps.LatLng(3.15681, 101.714696);
            var mapOptions = { zoom: 12, center: Centre, mapTypeId: google.maps.MapTypeId.ROADMAP };
            map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

            var KLCC = new google.maps.LatLng(3.15681, 101.714696);
            var marker = new google.maps.Marker({ position: KLCC, map: map, title: 'Kuala Lumpur City Center' });
            
        }
        google.maps.event.addDomListener(window, 'load', initializeGMap);
    
        function getInfo() {
            var contentString = '<div style="width: 400px; min-height: 100px;">';
            contentString += '<table style="font-size:12px; width:400px; height: 100px; border: none !important;">';
            contentString += '<tr style=\"background:none !important;\">';
            contentString += '<td valign=\"top\" style=\"width: 80px; height: 80px;border-bottom:none;\"></td>';
            contentString += '<td valign=\"top\" style=\"border-bottom:none;\">';
            contentString += '<table><tr style=\"background:none !important;\">';
            contentString += '<td style=\"font-size:16px;border-bottom:none;\">';
            contentString += '<b>Kuala Lumpur City Centre</b></td></tr>';
            contentString += '<tr style=\"background:none !important;\">';
            contentString += '<td style=\"border-bottom:none;\">';
            contentString += 'Kuala Lumpur City Centre<br />50450 Kuala Lumpur<br/>Wilayah Persekutuan Kuala Lumpur, Malaysia</td>';
            contentString += '</tr></table></td></tr><tr style=\"background:none !important;\">';
            contentString += '<td style=\"border-bottom:none;\">&nbsp;</td></tr>';
            contentString += '<tr style=\"background:none !important;border-bottom:none;\">';
            contentString += '<td colspan="2"></td></tr><tr style="background:none !important;">';
            contentString += '<td colspan="2" style=\"border-bottom:none;\"><b>Phone:</b>-</td>';
            contentString += '</tr></td></tr></table></div>';
            var infoWindowMarker = new google.maps.InfoWindow({
                content: contentString
            });
            var myLatLng = new google.maps.LatLng(3.15681, 101.714696);
            var myMarker = new google.maps.Marker({
                position: myLatLng,
                map: map,
                title: 'Kuala Lumpur City Centre'
            });
            infoWindowMarker.close();
            infoWindowMarker.open(map, myMarker);
            map.setCenter(myLatLng);
            map.setZoom(17);

        }
      </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div style="width: 600px; height: 400px; float:left;">
        <div id="map-canvas" style="width: 100%; height: 100%;">
        </div>
    </div>
    <div style="float:right;">
        <div id="Div1" style="width: 100%; height: 100%; text-align:left; padding:20px;">
            <asp:Button ID="Button1" runat="server" Text="Zoom The Marker" OnClientClick="getInfo();return false;" />
        </div>
    </div>
</asp:Content>

The Output 

Before Zoom

After Zoom


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.