Mursaleen Hassan

Mursaleen Hassan

  • NA
  • 22
  • 4.5k

Google Map Marker Array Error in Mvc and JavaScript

May 8 2018 2:56 PM
Please solve my problem. I am tired to pass SQL Server database value in Google Map marker Array in JavaScript Mvc. I am Run the code generate error "marker.length". In this code i will be showing Google Map Marker Cluster. I hope any one can help me. Thanks in Advance..
 
View
  1. @{    
  2.     ViewBag.Title = "Home Page";    
  3. }  
  4.     <div class="divdisplay">    
  5.         <script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer.js"></script>    
  6.         <script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js">    
  7.         </script>    
  8.     
  9.         <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>  
  10.         <script type="text/javascript">  
  11.             window.onload = function () {    
  12.                 var mapOptions = {    
  13.                     center: new google.maps.LatLng(25.359454, 68.357989),    
  14.                     zoom: 13,    
  15.                     mapTypeId: google.maps.MapTypeId.ROADMAP    
  16.                 };  
  17.                 var markers;    
  18.                 $(document).ready(function () {    
  19.                     $("#txt_button").on('click'function () {    
  20.     
  21.                         $.get("/Home/dates", { dt: '04-26-2018', city: 'sadgemoor' }, function (data) {    
  22.                             map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);    
  23.                             $.each(data, function (i, v1) {    
  24.                                 markers = [{ "title"'No Title'"lat": v1.b_latlng1, "lng": v1.b_latlng2, "description"'No Description' }]    
  25.                             });    
  26.                         });  
  27.                     });  
  28.                 });  
  29.                 // declare an array to keep all google.maps.Marker instances in:    
  30.                 var googleMarkers = [];  
  31.                 var infoWindow = new google.maps.InfoWindow();    
  32.                 var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);    
  33.                 for (i = 0; i < markers.length; i++) {    
  34.                     var data = markers[i]    
  35.                     var myLatlng = new google.maps.LatLng(data.lat, data.lng);    
  36.                     var marker = new google.maps.Marker({    
  37.                         position: myLatlng,    
  38.                         map: map,    
  39.                         title: data.title    
  40.                     });  
  41.                     // add the new marker to the googleMarkers array    
  42.                     googleMarkers.push(marker);   
  43.                     (function (marker, data) {    
  44.                         google.maps.event.addListener(marker, "click"function (e) {    
  45.                             infoWindow.setContent(data.description);    
  46.                             infoWindow.open(map, marker);    
  47.                         });    
  48.                     })(marker, data);    
  49.                 }  
  50.                 // now all the markers have been created, make a new MarkerClusterer:    
  51.                 var mcOptions = { gridSize: 50, maxZoom: 15, imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m' };    
  52.                 var markerCluster = new MarkerClusterer(map, googleMarkers, mcOptions);  
  53.             }    
  54.         </script>    
  55.         <button id="txt_button">Marker</button>    
  56.         <div id="dvMap" style="width: 900px; height: 500px">    
  57.         </div>   
  58.     </div>    
Crud Class 
  1. public class Crud_class  
  2.     {  
  3.         public string b_latlng1 { getset; }  
  4.         public string b_latlng2 { getset; }  
  5.     }  
DB class 
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Configuration;    
  4. using System.Data;    
  5. using System.Data.SqlClient;    
  6. using System.Linq;    
  7. using System.Web;    
  8.   
  9. namespace sql_cluster.Models    
  10. {    
  11.     public class db    
  12.     {    
  13.       public List<Crud_class> GetAllRoutes(DateTime dt,string city)    
  14.         {    
  15.             string strConnection = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;    
  16.             List<Crud_class> lstRoutes = new List<Crud_class>();    
  17.             using (SqlConnection con = new SqlConnection(strConnection))    
  18.             {    
  19.                 string sql = "select b_latlng from map_table where t_date='" + dt + "' and city='" + city + "'";    
  20.                 SqlCommand cmd = new SqlCommand(sql, con);    
  21.                 con.Open();    
  22.                 SqlDataReader reader = cmd.ExecuteReader();    
  23.                 Crud_class route = null;    
  24.                 while (reader.Read())    
  25.                 {    
  26.                     route = new Crud_class();    
  27.                     //route.id = Convert.ToInt32(reader["id"]);    
  28.                     route.b_latlng1 = (reader["b_latlng"].ToString().Split(',')[0]);    
  29.                     route.b_latlng2 = (reader["b_latlng"].ToString().Split(',')[1]);    
  30.                     //route.LatLngTo = reader["b_latlng"].ToString();    
  31.                     lstRoutes.Add(route);    
  32.                 }    
  33.             }    
  34.             return lstRoutes;    
  35.         }    
  36.     }    
  37. }  
Home Controller
  1. using sql_cluster.Models;    
  2. using System;    
  3. using System.Collections.Generic;    
  4. using System.Data;    
  5. using System.Linq;    
  6. using System.Web;    
  7. using System.Web.Mvc;    
  8.     
  9. namespace sql_cluster.Controllers    
  10. {    
  11.     public class HomeController : Controller    
  12.     {   
  13.         public ActionResult Index()    
  14.         {    
  15.             return View();    
  16.         }    
  17.     
  18.         public JsonResult dates(DateTime dt,string city)    
  19.         {    
  20.             List<Crud_class> lst = new db().GetAllRoutes(dt,city);    
  21.             return Json(lst, JsonRequestBehavior.AllowGet);    
  22.     
  23.         }    
  24.     
  25.     }    
  26. }