javascript - Google Geocoder - 将坐标添加到字段

标签 javascript google-maps google-maps-api-3 geocode

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

这是有效的代码: http://jsfiddle.net/Rr5PL/89/

如何将坐标放入长/纬度输入字段中?它必须存储它们,因为它使用它们在 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);
      }
    });
  }

最佳答案

您可以简单地从 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;

关于javascript - Google Geocoder - 将坐标添加到字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13211112/

相关文章:

javascript - 如何从地理编码地址中找到经纬度?

javascript - 在 Pixi.js 中旋转后沿 Y 轴缩放

javascript - HTML5 缓存行为不当

javascript - 查找最近的已知位置 : Google Reverse Geocoding

android - 谷歌地图代码不适用于 android - 没有 map

php - 使用 MySQL 和 group_concat 变量创建新行

javascript - 如何提示从嵌入 map 进入 Google map 导航器页面打开 Google map 路线?

javascript - 如何将图像路径从 AJAX 调用传递到 div 标签

javascript - 在二维区域内为对象寻找空间的算法

java - 如何在 Google map 中创建自定义 map ?