Share Blog

Monday, June 02, 2014

Adding Multiple Markers to Google Maps V3 using JSON with JavaScript in (Asp.net) Website

Introduction:

Here I will explain how to add multiple markers to Google maps API V3 using jQuery JSON in asp.net website or Show multiple markers in Google maps api in asp.net.

Description:
Now I will explain how to add multiple markers to Google maps api in asp.net website.
Once you got the key write the following code in your aspx or html page like as shown below
---------------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="google_location.aspx.cs" Inherits="google_location" %>

<!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>
<title>Show/Add multiple markers to Google Maps in asp.net website</title>
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript" src = "https://maps.googleapis.com/maps/api/js?key=AIzaSyC6v5-2uaq_wusHDktM9ILcqIrlPtnZgEk&sensor=false">
</script>
<script type="text/javascript">
    function initialize() {
        var markers = [
{
    "title": "Chennai",
    "lat": 12.897489183755905,
    "lng": 80.2880859375,
    "description": "Chennai"
},
{
    "title": "Hyderabad",
    "lat": 17.26672782352052,
    "lng": 78.5302734375,
    "description": "Hyderabad"
},
{
    "title": "Bangalore",
    "lat": 12.897489183755905,
    "lng": 77.51953125,
    "description": "Bangalore"
},
{
    "title": "visakhapatnam",
    "lat": 17.518344187852207,
    "lng": 83.3203125,
    "description": "visakhapatnam"
}];
        var mapOptions = {
            center: new google.maps.LatLng(11.44, 78.79),
            zoom: 5,
            mapTypeId: google.maps.MapTypeId.ROADMAP
            //  marker:true
        };
        var infoWindow = new google.maps.InfoWindow();
        var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
        for (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
            });
            (function (marker, data) {

                // Attaching a click event to the current marker
                google.maps.event.addListener(marker, "click", function (e) {
                    infoWindow.setContent(data.description);
                    infoWindow.open(map, marker);
                });
            })(marker, data);
        }
    }
</script>
</head>
<body onload="initialize()">
<form id="form1" runat="server">
<div id="map_canvas" style="width: 500px; height: 400px"></div>
</form>
</body>
</html>
--------------------------------------------------------------------------------------------
If you observe above code in header section I mentioned multiple latitude and longitudes of different cities and by default I mentioned one city latitude and longitude by using that default details we can center the map to near place of other cities based on that other cities whatever we are going to mark are visible.

In remaining code markers are used to add marking in google map and infowindow is used to show info of all the cities based on the description whatever we mentioned in json string.





Sunday, June 01, 2014

Show User Current Location on Google Map using GeoLocation API in Asp.net

Introduction:

Here I will explain how to show user current location using Google map API  (Geolocation) using JavaScript inasp.net website or Get user current location details (latitude and longitude) using Google map API (Geolocation) in JavaScript asp.net website.

Description:
Now I will explain how to show user current location using Google mapAPI (Geolocation) using JavaScript in asp.net website.
To show user current location details in Google map with latitude and longitude first we need to get Google API key for that please check below url

Once you got the key write the following code in your aspx or html page like as shown below 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="location_search.aspx.cs" Inherits="location_search" %>

<!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>
<title>Get User Current Location using Google Map Geolocation API Service in asp.net website</title>
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC6v5-2uaq_wusHDktM9ILcqIrlPtnZgEk&sensor=false">
</script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places">
</script>
<script type="text/javascript">
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(success);
    } else {
        alert("Geo Location is not supported on your current browser!");
    }
    function success(position) {
        var lat = position.coords.latitude;
        var long = position.coords.longitude;
        var city = position.coords.locality;
        var myLatlng = new google.maps.LatLng(lat, long);
        var myOptions = {
            center: myLatlng,
            zoom: 12,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        var marker = new google.maps.Marker({
            position: myLatlng,
            title: "lat: " + lat + " long: " + long
        });

        marker.setMap(map);
        var infowindow = new google.maps.InfoWindow({ content: "<b>User Address</b><br/> Latitude:" + lat + "<br /> Longitude:" + long + "" });
        infowindow.open(map, marker);
    }
</script>
</head>
<body >
<form id="form1" runat="server">
<div id="map_canvas" style="width: 500px; height: 400px"></div>
</form>
</body>
</html>



Start and Stop a Timer in JavaScript | Function Execution Start and Stop in JavaScript in Asp.net

Introduction

Here I will explain how to start and stop timer example inJavaScript or start and stop function execution using JavaScript in asp.net.

Description


To execute function repeatedly with fixed time delay we have a function with two parameters calledsetInterval(functionname, timedelay)
In this function we need to call the required function in functionname field and we need to set the required time delay to execute the function in timedelay field.
To stop the execution of repeated action we have function clearInterval(intervalID)
Here intervalID is the identifier of the repeated action you want to cancel. This ID is returned from setInterval().

If you want to see it in example you need to write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Run JavaScript function at specific intervals of time</title>
<script type="text/javascript">
var count=0,chkfn=null;
function changeColor() {
// Call function with 1000 milliseconds gap
chkfn = setInterval(starttimer, 1000);
}
function starttimer() {
count += 1;
var oElem = document.getElementById("divtxt");
oElem.style.color = oElem.style.color == "red" ? "blue" : "red";
document.getElementById("pcount").innerHTML = "Your Time Starts: " + count;
}
function stoptimer() {
clearInterval(chkfn);
chkfn = null;
count = 0;
document.getElementById("pcount").innerHTML = '';
}
</script>
</head>
<body>
<div id="divtxt">
<p id="pcount" style="font:bold 24px verdana"></p>
</div>
<button onclick="changeColor();">Start Timer</button>
<button onclick="stoptimer();">Stop Timer</button>
</body>
</html>