1

I'm trying to create a Gmap to allow people to geocode an address and find the respective lat lng. I would like to see this in the forms I've already created. Also, I would like to have the capability of changing the coordinates as the marker is dragged.

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="UTF-8">
    <title>Geocoder 10</title>
<style>
#map_canvas { width:400px; height:450px; }
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">

var geocoder;
var map;

function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-30.070, -51.190);
    var myOptions = {
        zoom: 9,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

function codeAddress() {
    var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                draggable: true,
            });

        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

</script>
</head>

<body onload="initialize()">
<div id="map_canvas"></div>
<div>
    <input id="address" type="textbox" value="">
    <input type="button" value="Localizar!" onclick="codeAddress()"><br>
    Latitude: <input type="text" id="lat"><br>
    Longitude: <input type="text" id="lng"><br>
    </div>
</body>
</html>
Daniel Montenegro
  • 1,491
  • 2
  • 15
  • 15
  • 1
    Isn't the `lat/lng` already inside `results[0].geometry.location`? :) – Ja͢ck Aug 14 '12 at 01:50
  • Could you explain it better, pls? I'm very new at this Gmaps Gecoding 'stuff'... if the value is already there, how can I show it in the forms? Thanks a lot! – Daniel Montenegro Aug 14 '12 at 01:58
  • 1
    let me google for an example. http://gmaps-samples.googlecode.com/svn/trunk/geocoder/singlegeocode.html/ – Jessedc Aug 14 '12 at 02:02

2 Answers2

2

Inside the geocoder call back do this:

document.getElementById('lat').value = results[0].geometry.location.lat();
document.getElementById('lng').value = results[0].geometry.location.lng();

Working example

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • Thanks a lot, Larry! Now, how could I drag the marker and get its new coordinates? Also, could you please explain what is a callback (in this context)? Do you know where can I find more examples and explanation on google geocoder (not from google documentation? – Daniel Montenegro Aug 14 '12 at 22:57
  • Might I suggest some research first? The question about getting coordinates from a draggable marker has been asked many times (here and in the Google Maps API v3 group) [Drag marker, get new coordinates](http://stackoverflow.com/questions/4645151/save-new-position-of-marker-from-draggable-marker-google-maps-v3), [Callback function](http://stackoverflow.com/questions/824234/what-is-a-callback-function) – geocodezip Aug 14 '12 at 23:42
  • Ok, I've already seen some questions/examples about that but I will search for more! – Daniel Montenegro Aug 15 '12 at 01:25
1

Ok... here's the code that makes what I wanted to. It might not be that elegant, but it works. I used the getPosition() method to get the coordinates, just have to click in the geocoded marker!

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="UTF-8">
    <title>Geocoder</title>
<style>
#map_canvas { width:400px; height:450px; }
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">

var geocoder;
var map;

function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-30.070, -51.190);
    var myOptions = {
        zoom: 9,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

function codeAddress() {
    var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                draggable: true,
            });

        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
        google.maps.event.addListener(marker, 'click', function() {
            document.getElementById('coords').value = marker.getPosition();
        });
    });
}

</script>
</head>

<body onload="initialize()">
<div id="map_canvas"></div>
<div id="info"></div>
<div>
    <input id="address" type="textbox" value="">
    <input type="button" value="Localizar!" onclick="codeAddress()"><br>
    <input type="textbox" id="coords">
    </div>
</body>
</html>
Daniel Montenegro
  • 1,491
  • 2
  • 15
  • 15