Google Map GPS Locator Using MVC And BootBox - Part Thirteen

Introduction

What Is Bootbox In Asp.net Mvc?

Bootbox.js is a small JavaScript library which allows you to create programmatic dialog boxes

using Bootstrap modals creating, managing or removing any of the required DOM elements or JS event handlers.

Bootbox.js is designed to make using Bootstrap modals easier.

Description
 
Here i will show you how to know your current location using google map GPS.

Then using marker you can find your current location coordinates.

To know more details about Google map , Visit My Blogs and articles.
  • http://www.c-sharpcorner.com/members/satyaprakash-samantaray 
Go through my previous blogs related to google map Part-12 before using this.
 
Steps To Be Followe,

Step1


Create a controller action method named "GPS()".
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace SatyaGoogleMapBootstrapMVC.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         public ActionResult GPS()  
  12.         {  
  13.             return View();  
  14.         }  
  15.     }  

Create a view named "GPS.cshtml".
  1. @{  
  2.     ViewBag.Title = "Satyaprakash Goolge Map Gps Locator";  
  3. }  
  4.   
  5. <title>@ViewBag.Title</title>  
  6.   
  7. <h2 style="background-color: Yellow;color: Blue; text-align: center; font-style: oblique">Satyaprakash's Google Map GPS Locator Using MVC and BootBox</h2>  
  8. <meta charset="utf-8">  
  9. <meta name="viewport" content="width=device-width, initial-scale=1">  
  10. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  
  11. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
  12. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
  13.   
  14.   
  15. <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>  
  16.   
  17. <script src="bootbox.min.js"></script>  
  18.   
  19. <style>  
  20.     #dvMap {  
  21.         height: 50%;  
  22.     }  
  23. </style>  
  24.   
  25. <script type="text/javascript">  
  26.     if (navigator.geolocation) {  
  27.         navigator.geolocation.getCurrentPosition(function (p) {  
  28.             var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);  
  29.             var mapOptions = {  
  30.                 center: LatLng,  
  31.                 zoom: 13,  
  32.                 mapTypeId: google.maps.MapTypeId.ROADMAP  
  33.             };  
  34.             var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);  
  35.             var marker = new google.maps.Marker({  
  36.                 position: LatLng,  
  37.                 map: map,  
  38.                 title: "<div style='font-size:large; font-family: Arial Black; background-color:yellow; color:blue; text-align: center'>Your Current Location Coordinates:<br />Latitude: " + p.coords.latitude + "<br />Longitude: " + p.coords.longitude  
  39.             });  
  40.             google.maps.event.addListener(marker, "click"function (e) {  
  41.                 //Using InfoWindow....  
  42.                 var infoWindow = new google.maps.InfoWindow();  
  43.                 infoWindow.setContent(marker.title);  
  44.                 infoWindow.open(map, marker);  
  45.                 //Using BootBox....  
  46.                 var msg = marker.title;  
  47.                 bootbox.alert(msg);  
  48.             });  
  49.         });  
  50.     } else {  
  51.         alert('Geo Location feature is not supported in this browser.');  
  52.     }  
  53. </script>  
  54.   
  55. <div id="dvMap">  
  56. </div>  
  57.   
  58. <footer>  
  59.     <p style="background-color: Yellow; font-weight: bold; color:blue; text-align: center; font-style: oblique">© @DateTime.Now.ToLocalTime()</p> @*Add Date Time*@  
  60. </footer> 
 I wrote code for my current location.
  1. navigator.geolocation.getCurrentPosition(function (p) {  
  2.             var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);  
  3.             var mapOptions = {  
  4.                 center: LatLng,  
  5.                 zoom: 13,  
  6.                 mapTypeId: google.maps.MapTypeId.ROADMAP  
  7.             }; 
 Then i configure the marker and pass div id of map.
  1. var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);  
  2.             var marker = new google.maps.Marker({  
  3.                 position: LatLng,  
  4.                 map: map,  
  5.                 title: "<div style='font-size:large; font-family: Arial Black; background-color:yellow; color:blue; text-align: center'>Your Current Location Coordinates:<br />Latitude: " + p.coords.latitude + "<br />Longitude: " + p.coords.longitude  
  6.             }); 
Then on click event of marker the current location coordinates will show using Info Window and BootBox Alert PopUp.
 
Using InfoWindow....
  1. google.maps.event.addListener(marker, "click"function (e) {  
  2.                 //Using InfoWindow....  
  3.                 var infoWindow = new google.maps.InfoWindow();  
  4.                 infoWindow.setContent(marker.title);  
  5.                 infoWindow.open(map, marker);  
  6. }); 
 Using BootBox....
  1. google.maps.event.addListener(marker, "click"function (e) {  
  2.                 //Using BootBox....  
  3.                 var msg = marker.title;  
  4.                 bootbox.alert(msg);  
  5.             }); 
 If location not found then warning message will come.
  1.  if (navigator.geolocation) {  
  2. //If Valid Geo Location there....  

  1. else {  
  2.         alert('Geo Location feature is not supported in this browser.');  
  3.     } 
 
I added BootBox Script File.
  1. <script src="bootbox.min.js"></script> 
 I added this zip file of "bootbox.min.js".
 
OUTPUT

Desktop View For InfoWindow and BootBox

 


Mobile View For InfoWindow and BootBox

 


Gif Images for Better Understanding Using InfoWindow and BootBox....

 
 
SUMMARY
  1. How to get current location.
  2. Get current location coordinates using bootbox and info window.
  3. Implement using mvc and bootstrap.
  4. implement in mobile will be more accurate than desktop to get your current location.