Google Map Location Coordinates Using Bootbox In ASP.NET MVC

Introduction

What Is Bootbox In Asp.net Mvc?

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

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

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

Image Reference

 
 

File reference

I attached one zip file to get “bootbox.min.js”.

Description

Here I used the Place changed event handler of the Google Autocomplete TextBox to get the selected place, its address and its location coordinates i.e. Latitude and Longitude.

To know more details about Google map , Visit My Blogs and articles.

http://www.c-sharpcorner.com/members/satyaprakash-samantaray

Steps To Be Followed

Step1

Create a Mvc 5 application named “SatyaGoogleMapBootstrapMVC”.



Step2

Create a controller class file named “HomeController.cs”.

Code Ref

  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.         //  
  12.         // GET: /Home/  
  13.         public ActionResult PlaceCord()  
  14.         {  
  15.             return View();  
  16.         }  
  17.     }  

Code Description

Here I created a controller action method named “PlaceCord()”.

  1. public ActionResult PlaceCord()  
  2.         {  
  3.             return View();  
  4.         } 
 

Step3

Add a Bootbox javascript library of version v4.4.0. Named “bootbox.min.js”.

This file I added to show alert pop up instead of traditional javascript alert box.

Also it is bootstrap supporting and multiplatform view support,

 

Step4

Create a view named “PlaceCord.cshtml”.

Code Ref

  1. @{  
  2.     ViewBag.Title = "Satyaprakash Goolge Map Coordinate";  
  3. }  
  4.   
  5. <title>@ViewBag.Title</title>  
  6.   
  7. <h2 style="background-color: Yellow;color: Blue; text-align: center; font-style: oblique">Satyaprakash's Goolge Map Place Coordinate Using MVC and BOOTSTRAP</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. <script src="bootbox.min.js"></script>  
  15.   
  16.   
  17. <style>      
  18.     input[type=text], select {  
  19.         width: 80%;  
  20.         padding: 12px 20px;  
  21.         margin: 10px 0;  
  22.         display: inline-block;  
  23.         border: 1px solid #ccc;  
  24.         border-radius: 4px;  
  25.         box-sizing: border-box;  
  26.   
  27.     }  
  28. </style>  
  29.   
  30. <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkVZYQFe4YYva_g5ulymGDt9EBoVjjZJ8&libraries=places">  
  31. </script>  
  32.   
  33.     <script type="text/javascript">  
  34.         google.maps.event.addDomListener(window, 'load'function () {  
  35.             var places = new google.maps.places.Autocomplete(document.getElementById('txtLoc'));  
  36.             google.maps.event.addListener(places, 'place_changed'function () {  
  37.                 var place = places.getPlace();  
  38.                 var address = place.formatted_address;  
  39.                 var latitude = place.geometry.location.lat();  
  40.                 var longitude = place.geometry.location.lng();  
  41.                 var mesg = "Location Address Is : " + address;  
  42.                 mesg += "\nLocation Latitude Is : " + latitude;  
  43.                 mesg += "\nLocation Longitude Is: " + longitude;  
  44.                 //alert(mesg); //For Normal Javascript Alert.  
  45.                 bootbox.alert(mesg); //For bootstrap Javascript Alert.  
  46.             });  
  47.         });  
  48.           
  49.     </script>  
  50.      
  51.     <input type="text" id="txtLoc" placeholder="Mention location...." />  
  52.   
  53.   
  54.     <footer>  
  55.         <p style="background-color: Yellow; font-weight: bold; color:blue; text-align: center; font-style: oblique">© @DateTime.Now.ToLocalTime()</p> @*Add Date Time*@  
  56.     </footer> 
Code Description

The HTML Markup consists of an HTML TextBox using which the Google Places Autocomplete will be implemented.

The very first thing is to inherit the Google Maps API script along with the places library. Then inside the Google Maps window load event handler, the Google Places Autocomplete is applied to the TextBox and then the place_changed event handler is assigned to it.

The place_changed event handler is triggered when a place is selected in the Google Places Autocomplete TextBox. It first gets the selected place and then determines its address and its location coordinates i.e. Latitude and Longitude and then displays the details in BootBox alert message box.

To support bootstrap related support added below links.

 

  1. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  
  2. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
  3. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
  4.   
  5. <script src="bootbox.min.js"></script> 
To show BootBox alert popup added this file path as described earlier.
  1. <script src="bootbox.min.js"></script> 

Instead of that I can use also below CDN link Wrappers for JavaScript alert(), confirm() and other flexible dialogs using the Bootstrap framework. CDN for web related libraries to speed up your websites!

  • https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.min.js
  • https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.js 
Added this cascading style sheet to give stylish look and bootstrap support to Place AutoComplete TextBox.
  1. <style>      
  2.     input[type=text], select {  
  3.         width80%;  
  4.         padding12px 20px;  
  5.         margin10px 0;  
  6.         display: inline-block;  
  7.         border1px solid #ccc;  
  8.         border-radius: 4px;  
  9.         box-sizing: border-box;  
  10.   
  11.     }  
  12. </style> 
Then I added my Google Api Key.
  1. <script src="https://maps.googleapis.com/maps/api/js?key=Put_Your_Api_key&libraries=places">  
  2. </script> 
Then I added a javascript code to show the place details with coordinates Using BootBox Alert PopUp.
  1. <script type="text/javascript">  
  2.         google.maps.event.addDomListener(window, 'load'function () {  
  3.             var places = new google.maps.places.Autocomplete(document.getElementById('txtLoc'));  
  4.             google.maps.event.addListener(places, 'place_changed'function () {  
  5.                 var place = places.getPlace();  
  6.                 var address = place.formatted_address;  
  7.                 var latitude = place.geometry.location.lat();  
  8.                 var longitude = place.geometry.location.lng();  
  9.                 var mesg = "Location Address Is : " + address;  
  10.                 mesg += "\nLocation Latitude Is : " + latitude;  
  11.                 mesg += "\nLocation Longitude Is: " + longitude;  
  12.                 //alert(mesg); //For Normal Javascript Alert.  
  13.                 bootbox.alert(mesg); //For bootstrap Javascript Alert.  
  14.             });  
  15.         });  
  16.           
  17.     </script> 
Add place details and coordinates and show using BootBox.
  1. var place = places.getPlace();  
  2.                 var address = place.formatted_address;  
  3.                 var latitude = place.geometry.location.lat();  
  4.                 var longitude = place.geometry.location.lng();  
  5.                 var mesg = "Location Address Is : " + address;  
  6.                 mesg += "\nLocation Latitude Is : " + latitude;  
  7.                 mesg += "\nLocation Longitude Is: " + longitude;  
  8.                 //alert(mesg); //For Normal Javascript Alert.  
  9.                 bootbox.alert(mesg); //For bootstrap Javascript Alert. 
Difference Between Normal javascript and BootBox Javascript Alert PopUp.
  1. alert(mesg); //For Normal Javascript Alert.  
  2. bootbox.alert(mesg); //For bootstrap Javascript Alert. 
Then I added a text box control and used its id to show autocomplete places using google map api key.
  1. google.maps.places.Autocomplete(document.getElementById('txtLoc'));  
  2. <input type="text" id="txtLoc" placeholder="Mention location...." /> 
Then I added footer to show current Date&Time.
  1. <footer>  
  2.         <p style="background-color: Yellow; font-weight: bold; color:blue; text-align: center; font-style: oblique">© @DateTime.Now.ToLocalTime()</p> @*Add Date Time*@  
  3.     </footer> 
 
 
Step5

To Set Start Page,

Code Ref

  1. routes.MapRoute(  
  2.                 name: "Default",  
  3.                 url: "{controller}/{action}/{id}",  
  4.                 defaults: new { controller = "Home", action = "PlaceCord", id = UrlParameter.Optional }  
  5.             ); 
Code Description

Here the controller name is “Home”.

Here name of the view is “PlaceCord”.

 

OUTPUT

URL IS - http://localhost:57237/Home/PlaceCord 

Dektop View

AutoComplete Places is shown ….  
 
 
The Place details with coordinates is shown using BootBox Alert PopUp.  
 
 

Mobile View
 
 

Difference Between Normal javascript and BootBox Javascript Alert PopUp.

Normal javascript

BootBox Javascript Alert PopUp

BootBox Is the most eye catching PopUp.

Gif Images for better understanding….

Summary

  1. What is BootBox.
  2. How to implement in Mvc.
  3. Difference Between Normal javascript and BootBox Javascript Alert PopUp.
  4. Get bootbox reference CDN link.


Similar Articles