Question

I am trying to find some minamilistic google geocoding script. The ones I have found are very large and complicated, but this one is very simple. The only thing it doesnt do is actually put the co-ordinates in two input boxes like the others.

Here is the code working: http://jsfiddle.net/Rr5PL/89/

How do I get it to put the co-ordinates in a long / lat input field? It must have them stored because it uses them to display on the map.

  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
  }

  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
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }
Was it helpful?

Solution

You can simply take them from the Javascript variables. You don't need input fields.

var lat = results[0].geometry.location.lat();
var lon = results[0].geometry.location.lng();
var url = 'www.url.com/search?lon='+lon+'&lat='+lat;

OTHER TIPS

In order to put latitude and longitude in text fields you need to create 2 text boxes first and put latitude and longitude.

var latitude= results[0].geometry.location.lat();
var longitude=results[0].geometry.location.lng();

This jsfiddle shows an example. http://jsfiddle.net/harendra/njDvn/6/

It's easy, try this

var loc=results[0].geometry.location;
document.getElementById('latLng').value=loc.lat()+', '+loc.lng();

DEMO.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top