Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 474.2k

Google maps show current location and state?

Mar 10 2020 7:27 AM
Hi Team
 
I need some help, my google app does not show correctly my current location, it must show South Africa, Port Elizabeth. But currently these co-ordinates are far from the continent in Africa which is not good for a user to see this, please help. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Data.SqlClient;  
  7. using System.Configuration;  
  8.   
  9. namespace eNtsaRegistrationTraining.Controllers  
  10. {  
  11.     public class MapsController : Controller  
  12.     {  
  13.         // GET: Maps  
  14.         public ActionResult GoogleMaps()  
  15.         {  
  16.             string markers = "[";  
  17.             string conString = ConfigurationManager.ConnectionStrings["eNtsaRegistration"].ConnectionString;  
  18.             SqlCommand cmd = new SqlCommand("SELECT * FROM Locations");  
  19.             using (SqlConnection con = new SqlConnection(conString))  
  20.             {  
  21.                 cmd.Connection = con;  
  22.                 con.Open();  
  23.                 using(SqlDataReader sdr = cmd.ExecuteReader())  
  24.                 {  
  25.                     while(sdr.Read())  
  26.                     {  
  27.                         markers += "{";  
  28.                         markers += string.Format("'title': '{0}',", sdr["Name"]);  
  29.                         markers += string.Format("'lat': '{0}',", sdr["Latitute"]);  
  30.                         markers += string.Format("'lng': '{0}',", sdr["Longitute"]);  
  31.                         markers += string.Format("'description': '{0}',", sdr["Description"]);  
  32.                         markers += "},";  
  33.                     }  
  34.                 }  
  35.                 con.Close();  
  36.             }  
  37.   
  38.             markers += "];";  
  39.             ViewBag.Markers = markers;  
  40.   
  41.             return View();  
  42.         }  
  43.     }  
  44. }  
 
  1. @{  
  2.     ViewBag.Title = "GoogleMaps";  
  3.     Layout = null;  
  4. }  
  5.   
  6. <!DOCYTPE html>  
  7. <html>  
  8. <head>  
  9.     <meta name="viewport" content="width=device-width"/>  
  10.     <title>eNtsa Office Location</title>  
  11. </head>  
  12. <body>  
  13.     <div id="dvMap" style="width: 500px; height: 500px">  
  14.     </div>  
  15.     <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCKm-dbDykfx2EIjKiZ0Wml8ehHep6K90M"></script>  
  16.     <script type="text/javascript">  
  17.         var markers = @Html.Raw(ViewBag.Markers);  
  18.         window.onload = function () {  
  19.             var mapOptions = {  
  20.                 center: new google.maps.LatLng(markers[0].lat, markers[0].lng),  
  21.                 zoom: 8,  
  22.                 mapTypeId: google.maps.MapTypeId.ROADMAP  
  23.             };  
  24.             var infoWindow = new google.maps.InfoWindow();  
  25.             var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);  
  26.             for (i = 0; i < markers.length; i++) {  
  27.                 var data = markers[i]  
  28.                 var myLatlng = new google.maps.LatLng(data.lat, data.lng);  
  29.                 var marker = new google.maps.Marker({  
  30.                     position: myLatlng,  
  31.                     map: map,  
  32.                     title: data.title  
  33.                 });  
  34.                 (function (marker, data) {  
  35.                     google.maps.event.addListener(marker, "click"function (e) {  
  36.                         infoWindow.setContent(data.description);  
  37.                         infoWindow.open(map, marker);  
  38.                     });  
  39.                 })(marker, data);  
  40.             }  
  41.         }  
  42.     </script>  
  43. </body>  
  44.   
  45. </html>  
 

Answers (6)