How to Search a Place in Map using Google Map API

Introduction

This article describes how to Search for a Place in Google Map using the Google Map API in ASP.Net. Here I will describe how to communicate with the Google Map API.

Description


To use the Google Map API you need to add the following link to the Head section.
  1. <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>  
And we need jquery file to pass the values to google map API.
 
Here I used jquery-1.9.1.js.

Design

Now add two TextBoxes, one Button, and one Label.

And div to show the map on the page.

Design your screen as in the following screen.


Or you can copy the following source code:
  1. <body>  
  2.     <form id="form1" runat="server">  
  3.     <div>  
  4.         <table>  
  5.             <tr>  
  6.                 <td colspan="2">  
  7.                     Search Place In Google Map  
  8.                 </td>  
  9.             </tr>  
  10.             <tr>  
  11.                 <td>  
  12.                     Country :  
  13.                 </td>  
  14.                 <td>  
  15.                     <asp:TextBox ID="txtCon" runat="server"></asp:TextBox>  
  16.                 </td>  
  17.             </tr>  
  18.             <tr>  
  19.                 <td>  
  20.                     City :  
  21.                 </td>  
  22.                 <td>  
  23.                     <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>  
  24.                 </td>  
  25.             </tr>  
  26.             <tr>  
  27.                 <td colspan="2" align="center">  
  28.                     <input id="btn" type="button" value="Search Place" />  
  29.                 </td>  
  30.             </tr>  
  31.         </table>  
  32.     </div>  
  33.     <div id="map-canvas" style="height: 500px; width: 500px">  
  34.     </div>  
  35.     </form>  
  36. </body>  
Now add the following jQuery and Google map link in the Head section:
  1. <script src="jquery-1.9.1.js" type="text/javascript"></script>  
  2. <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
Now  write the following JavaScript code in the Head section .
  1. <script type="text/javascript">  
  2.         $(document).ready(function () {  
  3.             $("#btn").click(function () {  
  4.                 var geocoder = new google.maps.Geocoder();  
  5.                 var con = document.getElementById('txtCon').value;  
  6.                 var city = document.getElementById('txtCity').value;  
  7.                 var com = city + "," + con;  
  8.                 geocoder.geocode({ 'address': com }, function (results, status) {  
  9.                     if (status == google.maps.GeocoderStatus.OK) {  
  10.                         var x = results[0].geometry.location.lat();  
  11.                         var y = results[0].geometry.location.lng();  
  12.                         var latlng = new google.maps.LatLng(x, y);  
  13.                         var myOptions = {  
  14.                             zoom: 8,  
  15.                             center: latlng,  
  16.                             mapTypeId: google.maps.MapTypeId.ROADMAP  
  17.                         };  
  18.                         map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);  
  19.                         var marker = new google.maps.Marker({  
  20.                             position: new google.maps.LatLng(x, y),  
  21.                             map: map,  
  22.                             title: com  
  23.                         });  
  24.                         var infowindow = new google.maps.InfoWindow({  
  25.                             content: com  
  26.                         });  
  27.                         infowindow.open(map, marker);  
  28.                         google.maps.event.addDomListener(window, 'load');  
  29.                     } else {  
  30.                         res.innerHTML = "Enter correct Details: " + status;  
  31.                     }  
  32.                 });  
  33.             });  
  34.         });  
  35. </script>
In the above code check these lines:
  1. <div id="map-canvas" style="height: 500px; width: 500px">  
  2. </div>  
The map to display on the page , we must need a place for it so we used this div to display the map.
  1. var x = results[0].geometry.location.lat();//latitude  
  2. var y = results[0].geometry.location.lng();//Longitude
In this line its getting the latitude and longitude value of the place you want to search.
  1. var latlng = new google.maps.LatLng(x, y);
In this, we are passing the latitude and longitude of the place to gmap.

mapTypeId: google.maps.MapTypeId.ROADMAP

In The above I used ROADMAP.

The following map types are supported:

    ROADMAP displays the normal, default 2D tiles of Google Maps.
    SATELLITE displays photographic tiles.
    HYBRID displays a mix of photographic tiles and a tile layer for prominent features (roads, city names).
    TERRAIN displays physical relief tiles for displaying elevation and water features (mountains, rivers, etc.).

Now build your application. Enter a City and Country in the text boxes then press the button.

Now It will show that place in the Google Map.



Similar Articles