M
M
maryoleksiuk2014-08-04 22:33:56
JavaScript
maryoleksiuk, 2014-08-04 22:33:56

How to convert coordinates to address on google maps?

I have a code .. I don’t know where to connect the codeLatLng () function correctly in order to get the address from the coordinates. Thanks

<!DOCTYPE html>
<html>
    <head>
        <title>Geolocation</title>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <style>
            html, body, #google_map{
            height: 100%;
            margin: 0px;
            padding: 0px;
        }
            
        #panel {
            position: absolute;
            top: 5px;
            left: 50%;
            margin-left: -180px;
            z-index: 5;
            background-color: #fff;
            padding: 5px;
            border: 1px solid #999;
        }
            
        </style>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>

        <script>
            var map;
            var geocoder;
            var marker;
            function initialize() {
                geocoder = new google.maps.Geocoder();
                var mapOptions = {
                zoom: 15,
                mapTypeId:google.maps.MapTypeId.ROADMAP
            };
            
            map = new google.maps.Map(document.getElementById('google_map'),
              mapOptions);
            
            if(navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function(position) {
                    var geocoder = new google.maps.Geocoder();
                    var lat = position.coords.latitude;
                    var lng = position.coords.longitude;
                    var latlng = new google.maps.LatLng(lat, lng);

                    var infowindow = new google.maps.InfoWindow({
                        content: 
                        'Location'
                    });
                    
                    var marker = new google.maps.Marker({
                        map: map,
                        position: latlng,
                        title: 'You are here!' + codecontent       
                    });
                    
                    google.maps.event.addListener(marker, 'click', function () {
                        // Calling the open method of the infoWindow 
                        infowindow.open(map, marker);
                    });
                    
                    map.setCenter(latlng);
                }, function() {
                    handleNoGeolocation(true);
                });
            } else {
                // Browser doesn't support Geolocation
                handleNoGeolocation(false);
            }
        }
            
                 
            
        //Reverse geocode
            
        function codeLatLng() {
              var input = document.getElementById('latlng').value;
              var latlngStr = input.split(',', 2);
              var lat = parseFloat(latlngStr[0]);
              var lng = parseFloat(latlngStr[1]);
              var latlng = new google.maps.LatLng(lat, lng);
              geocoder.geocode({'latLng': latlng}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                  if (results[1]) {
                    map.setZoom(11);
                    marker = new google.maps.Marker({
                        position: latlng,
                        map: map
                    });
                    infowindow.setContent(results[1].formatted_address);
                    infowindow.open(map, marker);
                  } else {
                    alert('No results found');
                  }
                } else {
                  alert('Geocoder failed due to: ' + status);
                }
              });
            }
            
        //End of reverse geocode

        function handleNoGeolocation(errorFlag) {
            if (errorFlag) {
                var content = 'Error: The Geolocation service failed.';
            } else {
                var content = 'Error: Your browser doesn\'t support geolocation.';
            }

            var options = {
                map: map,
                position: new google.maps.LatLng(60, 105),
                content: content
            };

            var infowindow = new google.maps.InfoWindow(options);
            map.setCenter(options.position);
        }
               
        google.maps.event.addDomListener(window, 'load', initialize);
            

    </script>
  </head>
  <body>
    <div id="google_map"></div>
  </body>
</html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2014-08-04
@0neS

what?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question