Jul 24, 2015

Getting The Address Of Selected Location In Google Map Using Google Map API v3

Following Example Will Help You To Develop Your Own Google Map To Get The Address Of A Location Including Latitude And Longitude. Before Using Make Sure You have Downloaded The "jquery.gmap3.min.js" Java Script File.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<html>
<head>
    <title>Google Map v3</title>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false& libraries=places"></script>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.1.min.js"></script>
</head>
<body>
    <div id="map-canvas" style="width: 100%; height: 500px;"></div>
    Street :<input type="text" id="street" />
    City :<input type="text" id="city"/>
    State :<input type="text" id="state" />
    Country :<input type="text" id="country" />
    Latitude :<input type="text" id="lat" />
    Longitude :<input type="text" id="lng"/>

    <script type="text/javascript">

         var componentForm = {
           street_number: 'short_name',
           route: 'long_name',
           locality: 'long_name',
           administrative_area_level_1: 'long_name',
           country: 'long_name'
         };

         var map;
         var lat = 6.929537;//Set Default Latitude To Start
         var lon = 79.866271;//Set Default Longitude To Start
         var str = '[{ "lat" :"' + lat + '","lng" :"' + lon + '"}]';
         str = JSON.parse(str);
       
         jQuery('#map-canvas').gmap3({
            marker: {
               values: str,
               options: {
                 icon:'http://maps.google.com/mapfiles/ms/icons/red-dot.png',
                 draggable:true
               },
               events:{
                 dragend: function(marker){
                   $('#lat').val(marker.getPosition().lat());
                   $('#lng').val(marker.getPosition().lng());
                   $(this).gmap3({
                     getaddress:{
                       latLng:marker.getPosition(),
                       callback:function(results){
                         printAddress(results[0]);
                       }
                     }
                   });
                 }
               },
            },
            map: {
               options: {
                 zoom: 14,
                 scrollwheel: true,
                 streetViewControl: true
               }
            }
        });

        function printAddress(place){
            var streetnumber = "";
            var streetname = "";
            for (var i = 0; i < place.address_components.length; i++) {
                var addressType = place.address_components[i].types[0];
                if(addressType == "street_number"){
                  streetnumber = place.address_components[i][componentForm[addressType]];
                }else if(addressType == "route"){
                  streetname = place.address_components[i][componentForm[addressType]];
                }else if(addressType == "locality"){
                  $("#city").val(place.address_components[i][componentForm[addressType]]);
                }else if(addressType == "administrative_area_level_1"){
                  $("#state").val(place.address_components[i][componentForm[addressType]]);
                }else if(addressType == "country"){
                  $("#country").val(place.address_components[i][componentForm[addressType]]);
                }
                if(streetnumber != "" && streetname !=""){
                  $("#street").val(streetnumber + " , " + streetname);
                }
                else{
                  $("#street").val(streetnumber + streetname);
                }
            }
        }
    </script>
</body>
</html>




This Is Simple Example That Gets You The Address Of A Selected Place Using Google Map.You Can Drag The Marker Position As You Want.Then It Will Get You The Updated Marker Position And It Will Give You The Address Of The Location.This Uses A 'Dragend' Function.There Are Lot Of Functions Associated With Google Maps.


Check My Other Articles About Google Maps.

1. Creating A Google Map Using Google Map API v3
2. Show Current Location In A Google Map Using Google Map API v3
3. Getting The Address Of Selected Location In Google Map Using Google Map API v3
4. Getting The Selected Latitude And Longitude From Google Map Using Google Map API v3
5. Google Map With Multiple Markers & Info Windows Using Google Map API v3
6. Getting The Direction Between Two Markers In Google Map Using Google MAP API
7. Getting The Direction Between Two Locations Using Google MAP API
8. Getting The Distance Between Two Markers In Google Map Using Google MAP API
9. Getting The Distance Between Two Locations Using Google MAP API
10. Getting The Nearest Places For A Location In Google Map Using Google MAP API
11. Google Map With InfoBubble
12. Google Maps Creating Polygon And Retrieving Coordinates

No comments:

JWT Token Decode Using Jquery

When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...