Tuesday 14 May 2013

Google map integration in asp.net example


Google map integration in asp.net example



Here you can find simple solution to Google map integration in asp.net example. In current version there is no need to use API key to identify form which domain the request is coming.

Use this simple code and modify the latitude and longitude according to your need. This is just 5min work.



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GoogleMap.aspx.cs"Inherits="AspNetDemo.GoogleMap" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>Google Maps Multiple Markers</title>
    <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>   
</head>
<body>
    <center>
        <div id="MyMap" style="width: 500px; height: 400px; margin-top: 50px;">
        </div>
    </center>   
    <script type="text/javascript">

        var map;
        var icon0;
        var newPoints = new Array();
        function addLoadEvent(func) {
            var oldonload = window.onload;
            if (typeof window.onload != 'function') {
                window.onload = func
            } else {
                window.onload = function () {
                    oldonload();
                    func();
                }
            }
        }

        addLoadEvent(initialize);
        function initialize() {
            var myOptions = {
                zoom: 4,
                center: new google.maps.LatLng(20.593684, 78.96288),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            var map = new google.maps.Map(document.getElementById("MyMap"), myOptions);
            setMarkers(map, locations);
        }

        /**
        * Data for the markers consisting of a name, a LatLng and a zIndex for
        * the order in which these markers should display on top of each
        * other.
        */
        var locations = [
  ['<strong>Pune.</strong>,</br> Maharashtra</br>India.', 18.520430, 73.856744, 1],
  ['<strong>Mumbai.</strong>,</br> Maharashtra</br>India.', 19.075984, 72.877656, 2]
];

        function setMarkers(map) {
            // Add markers to the map

            // Marker sizes are expressed as a Size of X,Y
            // where the origin of the image (0,0) is located
            // in the top left of the image.

            // Origins, anchor positions and coordinates of the marker
            // increase in the X direction to the right and in
            // the Y direction down.
            var image = new google.maps.MarkerImage('http://www.google.com/mapfiles/marker.png',
            // This marker is 20 pixels wide by 32 pixels tall.
              new google.maps.Size(20, 32),
            // The origin for this image is 0,0.
              new google.maps.Point(0, 0),
            // The anchor for this image is the base of the flagpole at 0,32.
              new google.maps.Point(0, 32));
            var shadow = new google.maps.MarkerImage('http://www.google.com/mapfiles/shadow50.png',
            // The shadow image is larger in the horizontal dimension
            // while the position and offset are the same as for the main image.
              new google.maps.Size(37, 32),
              new google.maps.Point(0, 0),
              new google.maps.Point(0, 32));
            // Shapes define the clickable region of the icon.
            // The type defines an HTML <area> element 'poly' which
            // traces out a polygon as a series of X,Y points. The final
            // coordinate closes the poly by connecting to the first
            // coordinate.
            var shape = {
                coord: [1, 1, 1, 20, 18, 20, 18, 1],
                type: 'poly'
            };
            for (var i = 0; i < locations.length; i++) {
                var beach = locations[i];
                var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
                var marker = new google.maps.Marker({
                    position: myLatLng,
                    map: map,
                    shadow: shadow,
                    icon: image,
                    shape: shape,
                    //title: beach[0],
                    zIndex: beach[3]
                });
                var infowindow = new google.maps.InfoWindow();
                var marker, i;
                google.maps.event.addListener(marker, 'mouseover', (function (marker, i) {
                    return function () {
                        infowindow.setContent(locations[i][0]);
                        infowindow.open(map, marker);
                    }
                })(marker, i));
            }
        }
    </script>
</body>
</html>


Once you run this code you will see output like this

 
 
 In this way Google map integration can be done in asp.net with this simple example.

No comments:

Post a Comment