ASP.NET - how to draw a line from one location to another using c# in googleMap

Asked By balaji mogadali on 15-Oct-11 05:40 AM
hi frds

How to draw a line from one location to another using c# in googleMap

pls help

smr replied to balaji mogadali on 15-Oct-11 05:43 AM
HI


follow  http://www.codeproject.com/KB/aspnet/plotpath_gps.aspx

try this


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
 
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Plot Path Demo Using Google Maps JavaScript API v3</title>
<link
href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css"
rel="stylesheet" type="text/css" />
 
<!--Google API Javascript Reference-->
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=false"></script>
 
<!--Literal Control for runtime javascript-->
<asp:Literal ID="js" runat="server"></asp:Literal>
</head>
<body onload="initialize()">
  <form id="form1" runat="server">
      <!--Canvas for showing the google map-->
       <div id="map_canvas" style="width: 100%; height: 100%;"></div>
   </form>
</body>
</html>




public static String PlotGPSPoints(DataTable tblPoints)
  {
    try
    {
      String Locations = "";
      String sJScript = "";
      int i = 0;
      foreach (DataRow r in tblPoints.Rows)
      {
        // bypass empty rows
        if (r["latitude"].ToString().Trim().Length == 0)
          continue;
 
        string Latitude = r["latitude"].ToString();
        string Longitude = r["longitude"].ToString();
 
        // create a line of JavaScript for marker on map for this record
        Locations += Environment.NewLine + @"
        path.push(new google.maps.LatLng(" + Latitude + ", " + Longitude + @"));
 
        var marker" + i.ToString() + @" = new google.maps.Marker({
          position: new google.maps.LatLng
      (" + Latitude + ", " + Longitude + @"),
          title: '#' + path.getLength(),
          map: map
        });";
        i++;
      }
 
      // construct the final script
      sJScript = @"<script type="text/javascript">
 
               var poly;
               var map;
 
               function initialize() {
                 var cmloc = new google.maps.LatLng
          (33.779005, -118.178985);
                 var myOptions = {
                   zoom: 11,
                   center: cmloc,
                   mapTypeId: google.maps.MapTypeId.ROADMAP
                 };
 
                 map = new google.maps.Map
        (document.getElementById('map_canvas'), myOptions);
 
                 var polyOptions = {
                   strokeColor: 'blue',
                   strokeOpacity: 0.5,
                   strokeWeight: 3
                 }
                 poly = new google.maps.Polyline(polyOptions);
                 poly.setMap(map);
 
                 var path = poly.getPath();
 
                 " + Locations + @"
               }
        </script>";
      return sJScript;
    }
    catch (Exception ex)
    {
      throw ex;
    }
  }

map.png
Suchit shah replied to balaji mogadali on 15-Oct-11 05:45 AM
  • Create points for the polyline:
  • GooglePoint GP1 = new GooglePoint();
    GP1.ID = "GP1";
    GP1.Latitude = 43.65669;
    GP1.Longitude = -79.44268;
    GP1.InfoHTML = "This is point 1";
    GoogleMapForASPNet1.GoogleMapObject.Points.Add(GP1);
    GooglePoint GP2 = new GooglePoint();
    GP2.ID = "GP2";
    GP2.Latitude = 43.66619;
    GP2.Longitude = -79.44268;
    GP2.InfoHTML = "This is point 2";
    GoogleMapForASPNet1.GoogleMapObject.Points.Add(GP2);
    GooglePoint GP3 = new GooglePoint();
    GP3.ID = "GP3";
    GP3.Latitude = 43.67689;
    GP3.Longitude = -79.43270;
    GP3.InfoHTML = "This is point 3";
    GoogleMapForASPNet1.GoogleMapObject.Points.Add(GP3);
  • Create a polyline between points GP1, GP2, and GP3:
  • //Define polyline
    GooglePolyline PL1 = new GooglePolyline();
    PL1.ID = "PL1";
    //Give Hex code for line color
    PL1.ColorCode = "#0000FF";
    //Specify width for line
    PL1.Width = 5;
    //Add points
    PL1.Points.Add(GP1);
    PL1.Points.Add(GP2);
    PL1.Points.Add(GP3);
  • Add a polyline to the Google Maps control:
  • GoogleMapForASPNet1.GoogleMapObject.Polylines.Add(PL1);


    Check out here it will tell u step by step everything for ur requirement
    http://www.codeproject.com/KB/custom-controls/Google-Maps-User-Control.aspx




    Chintan Vaghela replied to balaji mogadali on 15-Oct-11 08:51 AM

    Hello,

     

    Try following approach

    Need to use GoogleMapAPI


     

     

     

    Hope this is helpful !

     

    Thanks