0

我正在尝试找到一些极简主义的谷歌地理编码脚本。我发现的很大很复杂,但是这个很简单。它唯一没有做的实际上是将坐标放在两个输入框中,就像其他输入框一样。

这是代码工作:http: //jsfiddle.net/Rr5PL/89/

如何将坐标放在长/纬度输入字段中?它必须将它们存储起来,因为它使用它们在地图上显示。

  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);
      }
    });
  }
4

3 回答 3

1

为了在文本字段中输入纬度和经度,您需要先创建 2 个文本框并输入纬度和经度。

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

这个 jsfiddle 展示了一个例子。 http://jsfiddle.net/harendra/njDvn/6/

于 2012-11-03T16:41:43.497 回答
1

很简单,试试这个

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

演示

于 2012-11-03T16:42:26.163 回答
1

您可以简单地从 Javascript 变量中获取它们。您不需要输入字段。

var lat = results[0].geometry.location.lat();
var lon = results[0].geometry.location.lng();
var url = 'www.url.com/search?lon='+lon+'&lat='+lat;
于 2012-11-03T16:46:59.787 回答