【问题标题】:How to enter street address directly in the code如何在代码中直接输入街道地址
【发布时间】:2013-03-19 05:41:40
【问题描述】:

我想像这个例子中一样使用谷歌地图 api v3 https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple

但没有这个输入字段

<div>
  <input id="address" type="textbox" value="Sydney, NSW">
  <input type="button" value="Geocode" onclick="codeAddress()">
</div>

我想直接在代码中指定街道地址,例如“75 wall st, new york”。我该怎么做呢?以下是该示例网址中的代码。有人知道需要删除/添加什么吗?

<link href="https://google-developers.appspot.com/maps/documentation/javascript/examples/default.css" rel="stylesheet">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
  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);
      }
    });
  }
</script>
</head>
<body onload="initialize()">
<div>
<input id="address" type="textbox" value="Sydney, NSW">
<input type="button" value="Geocode" onclick="codeAddress()">
</div>
<div id="map-canvas" style="height:90%;top:30px"></div>

【问题讨论】:

    标签: google-maps-api-3 geocoding


    【解决方案1】:

    我想你可以更新函数来做到这一点:

    function codeAddress(address) {
    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);
      }
    });
    

    }

    一旦你想更新地图,只需调用:

    codeAddress('75 wall st, new york');
    

    如果你使用 jQuery,你可以把它放在你的文档就绪函数中,或者你的代码的另一部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      相关资源
      最近更新 更多