- LatLngBounds
- fitBounds
- getBounds
- setBounds
- getPlaces
Using the preceding JavaScript methods you can integrate Google Maps Search Places into your application. The following are some of the features of a Google Maps Place Autocomplete search.
Key points for integrating Google Maps:
- Google provides the free JavaScript API to integrate Google Maps into our applications.
- You can customize Google Maps depending on your requirements.
- its provides the suggestion while typing.
Let us see the preceding points with an example by creating a web application using the following:
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New WebSite" - "C#" - "ASP.NET Empty Web Application" (to avoid adding a master page).
- Provide the web site a name such as "GoogleLocationSearch" or another as you wish and specify the location.
- Then right-click on Solution Explorer - "Add New Item" then Default.aspx page.
Now the Default.aspx source code will look as in the following:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
- Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
- <meta charset="utf-8">
- <title>Article by Vithal Wadje</title>
- </head>
- <body bgcolor="Blue">
- <h5 style="color:White;">
- Article for C# corner</h5>
- </body>
- </html>
- <input id="txtsearch" class="apply" type="text" placeholder="Enter Search Place e.g C# Corner Noida">
- <div id="divloadMap"></div>
- html, body,#divloadMap {
- height: 100%;
- margin: 0px;
- padding: 0px;
- margin-top:15px
- }
- .apply {
- margin-top: 16px;
- border: 1px solid transparent;
- border-radius: 2px 0 0 2px;
- box-sizing: border-box;
- -moz-box-sizing: border-box;
- height: 32px;
- outline: none;
- box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
- }
- #txtsearch {
- background-color: #fff;
- padding: 0 11px 0 13px;
- width: 400px;
- font-family: Roboto;
- font-size: 15px;
- font-weight: 300;
- text-overflow: ellipsis;
- }
- #txtsearch:focus {
- border-color: #4d90fe;
- margin-left: -1px;
- padding-left: 14px;
- width: 401px;
- }
- .pac-container {
- font-family: Roboto;
- }
- #type-selector {
- color: #fff;
- background-color: #4d90fe;
- padding: 5px 11px 0px 11px;
- }
- #type-selector label {
- font-family: Roboto;
- font-size: 13px;
- font-weight: 300;
- }

- //creating function to load initial MAP when page loading
- function LoadGoogleMAP() {
- var markers = [];
- var map = new google.maps.Map(document.getElementById('divloadMap'), {
- mapTypeId: google.maps.MapTypeId.ROADMAP
- });
- var defaultBounds = new google.maps.LatLngBounds(
- new google.maps.LatLng(-33.8902, 151.1759),
- new google.maps.LatLng(-33.8474, 151.2631));
- map.fitBounds(defaultBounds);
- // Create the search box and link it to the UI element.
- var input =(document.getElementById('txtsearch'));
- map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
- var searchBox = new google.maps.places.SearchBox((input));
- // Listen for the event fired when the user selects an item from the
- // pick list. Retrieve the matching places for that item.
- google.maps.event.addListener(searchBox, 'places_changed', function () {
- var places = searchBox.getPlaces();
- if (places.length == 0) {
- return;
- }
- for (var i = 0, marker; marker = markers[i]; i++) {
- marker.setMap(null);
- }
- // For each place, get the icon, place name, and location.
- markers = [];
- var bounds = new google.maps.LatLngBounds();
- for (var i = 0, place; place = places[i]; i++) {
- var image = {
- url: place.icon,
- size: new google.maps.Size(71, 71),
- origin: new google.maps.Point(0, 0),
- anchor: new google.maps.Point(17, 34),
- scaledSize: new google.maps.Size(25, 25)
- };
- // Create a marker for each place.
- var marker = new google.maps.Marker({
- map: map,
- icon: image,
- title: place.name,
- position: place.geometry.location
- });
- markers.push(marker);
- bounds.extend(place.geometry.location);
- }
- map.fitBounds(bounds);
- });
- // current map's viewport.
- google.maps.event.addListener(map, 'bounds_changed', function () {
- var bounds = map.getBounds();
- searchBox.setBounds(bounds);
- });
- }
- google.maps.event.addDomListener(window, 'load', LoadGoogleMAP);
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
- Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
- <meta charset="utf-8">
- <title>Article by Vithal Wadje</title>
- <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
- <script src="GoogleAPIScript.js" type="text/javascript"></script>
- <link href="MapStyleSheet.css" rel="stylesheet" type="text/css" />
- </head>
- <body bgcolor="Blue">
- <h5 style="color:White;">
- Article for C# corner</h5>
- <input id="txtsearch" class="apply" type="text" placeholder="Enter Search Place e.g C# Corner Noida">
- <div id="divloadMap"></div>
- </body>
- </html>




- Download the source code of the application for a detailed understanding.
- You need an active internet connection to view Google Maps.
Summary


Saravanakumar SekaranPosted Aug 11, 2021, 5:28 AM EDT
Awesome article. let me know google map is free ?
sachin bhaleraoPosted Oct 2, 2019, 8:17 PM EDT
Dear Vithal , after executing it is showing error "This page can't load Google Maps correctly." Please suggest me some solution
Prasanna LaadPosted Apr 5, 2019, 12:34 AM EDT
Sir, the search bar is not working, means the suggestions are not displayed. can you guide me please?
Rajeev KumarPosted Dec 13, 2018, 3:13 AM EST
Hi, Can you please guide me .net code for trip planner. Like gets possible routes on the base of Source Location and Destination. Thanks
Manjunath BiradarPosted Oct 12, 2016, 3:07 AM EDT
Thanks,How to add marks from database in this map
Vithal WadjeTOP 100Posted Dec 19, 2015, 3:50 AM EST
Thanks Anu Vivin
Anu VPosted Dec 18, 2015, 10:41 PM EST
good one..
Raju SanPosted Sep 6, 2015, 10:11 PM EDT
Hello sir downlaod link giving some error plz help
Vithal WadjeTOP 100Posted Jun 9, 2015, 8:37 AM EDT
Rahul Sharma sir refer this LatLngBounds fitBounds getBounds setBounds getPlaces in article
Vithal WadjeTOP 100Posted Jun 9, 2015, 8:36 AM EDT
Thanks Gilbert Santos
Rahul SharmaPosted Jun 9, 2015, 12:15 AM EDT
How to dynamically add LatLng to google map api, i means to say i want to save latlong values in ms sql server and and want to show map depending on the page(insert values dynamivally)
Gilbert SantosPosted Jun 8, 2015, 6:56 PM EDT
Thanks for the sample.. I've been looking a bit long now for this kind of GOOGLE MAP usage...Great job... Looking forward to see more of your work sooner...Tnx man...
Vithal WadjeTOP 100Posted Feb 24, 2015, 4:23 AM EST
Thanks Rahul Saxena sir
Vithal WadjeTOP 100Posted Feb 24, 2015, 4:23 AM EST
thanks a lot Dinesh Beniwal sir
Rahul Kumar SaxenaTOP 100Posted Feb 23, 2015, 7:28 PM EST
Congrts Vithal Wadje...
Dinesh BeniwalTOP 10Posted Feb 23, 2015, 5:34 PM EST
Congrats Vithal Wadje Article of the day on ASP.NET
Vithal WadjeTOP 100Posted Jan 6, 2015, 12:51 AM EST
google searcher map textbox means you wants only location details instead of showing on MAP ? rigth
Muhammad SaroshPosted Jan 5, 2015, 9:11 PM EST
this is very intresting but i dont wanna required map but only google searcher map textbox please help me shall be very thankful to you please consult on me [email protected]
Vithal WadjeTOP 100Posted Dec 14, 2014, 5:07 PM EST
thanks to moderators and judges for making this article feature
Vithal WadjeTOP 100Posted Dec 14, 2014, 8:24 AM EST
Thanks Michal Habalcik sir for your great words
Vithal WadjeTOP 100Posted Dec 14, 2014, 8:23 AM EST
thanks once again Jitendra Kumar sir
Vithal WadjeTOP 100Posted Dec 14, 2014, 8:21 AM EST
thanks Manish Kumar Choudhary sir
Michal HabalcikPosted Dec 14, 2014, 4:27 AM EST
excellent tutorial, thanks
Jitendra KumarPosted Dec 14, 2014, 3:22 AM EST
Good...
Manish Kumar ChoudharyPosted Dec 14, 2014, 1:53 AM EST
nice one....