【问题标题】:Trouble getting google map to update location through geocoding a users search无法通过对用户搜索进行地理编码来更新位置的谷歌地图
【发布时间】:2016-05-22 23:57:22
【问题描述】:

当用户点击提交时,我正在尝试使用 mapSearch 框中的用户输入来更新我的地图,但我遗漏了未将提交 onclick 函数与 latlng 变量连接起来的内容。

var geocoder;
var map;
function initialize() {

geocoder = new google.maps.Geocoder();
var latlng =new google.maps.LatLng(-34.397,150.644);

   var mapProp = {
    center: latlng, 
    }
var map=new google.maps.Map(document.getElementById("googleMap"), mapProp);

  };

var searchBox = new google.maps.places.SearchBox(document.getElementById('mapsearch'));

google.maps.event.addDomListener(searchBox, 'places_changed', function(){

  var places = searchBox.getPlaces();

  var bounds = new google.maps.LatLngBounds();
  var i, place;

  for (i=0; place=places[i]; i++){

    bounds.extend(place.geometry.location);

  }
});


$( "#Submit" ).click(function codeAddress(){
  var address = document.getElementById("mapsearch").value;
  geocoder.geocode( {'mapsearch': 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 错误:` InvalidValueError: unknown property mapsearch` 在这一行:` geocoder.geocode( {'mapsearch': address},` (应该是 'address' )
  • 我试图将它缩短到我认为是问题的部分,但看起来这是一个新手错误:这是完整的代码

标签: google-maps-api-3 google-geocoding-api


【解决方案1】:

我在发布的代码中收到一个 javascript 错误:InvalidValueError: unknown property mapsearch 在此行:geocoder.geocode( {'mapsearch': address},(应该是“地址”):

$("#Submit").click(function codeAddress() {
  var address = document.getElementById("mapsearch").value;
  geocoder.geocode({
    'address': address
  },

代码 sn-p:

var geocoder;
var map;

function initialize() {

  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(-34.397, 150.644);

  var mapProp = {
    center: latlng,
    zoom: 3
  }

  var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);



  $("#Submit").click(function codeAddress() {
    var address = document.getElementById("mapsearch").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);
        }
      });
  });
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#googleMap {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<input id="Submit" type="button" value="submit" />
<input id="mapsearch" value="New York, NY" />
<div id="googleMap"></div>

【讨论】:

    猜你喜欢
    • 2012-12-30
    • 2017-04-19
    • 2014-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 2013-01-13
    • 1970-01-01
    相关资源
    最近更新 更多