0

所以我在 open.php 中有一个字段地址,当用户开始写一些东西时,会自动完成谷歌地址。当用户选择地址时,地图上会放置一个标记,并填充纬度/经度字段。

现在我增加了用户在前一页填写地址字段的可能性,然后 open.php 从 url 获取地址:

<input id="address" type="text" value="<?php echo $_GET['query']; ?>"/>

但是随后 JS 脚本将不会运行来填充地图、纬度和经度。我该如何解决这个问题?

(我需要用户在前一页填写地址字段的可能性,因为用户可以直接在 open.php 上输入)

谢谢!

来自 JS 的代码:

var geocoder;
var map;
var marker;

function initialize(){
//MAP
  var latlng = new google.maps.LatLng(49.599,6.134);
  var options = {
    zoom: 14,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.HYBRID
  };

  map = new google.maps.Map(document.getElementById("map_canvas"), options);

  //GEOCODER
  geocoder = new google.maps.Geocoder();

  marker = new google.maps.Marker({
    position: latlng,
    map: map,
    animation: google.maps.Animation.BOUNCE,
    draggable: true
  });

}

$(document).ready(function() { 

  initialize();

  $(function() {
    $("#address").autocomplete({
      //This bit uses the geocoder to fetch address values
      source: function(request, response) {
      // geocoder.geocode( {'address': request.term }, function(results, status) {
        geocoder.geocode( {'address': request.term + ', lu' }, function(results, status) {
          response($.map(results, function(item) {
            return {
              label:  item.formatted_address,
              value: item.formatted_address,
              latitude: item.geometry.location.lat(),
              longitude: item.geometry.location.lng()
            }
          }));
        })
      },
      //This bit is executed upon selection of an address
      select: function(event, ui) {
        $("#latitude").val(ui.item.latitude);
        $("#longitude").val(ui.item.longitude);
        var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
        marker.setPosition(location);
        map.setCenter(location);
      }
    });
  });

  //Add listener to marker for reverse geocoding
  google.maps.event.addListener(marker, 'drag', function() {
    geocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        if (results[0]) {
          $('#address').val(results[0].formatted_address);
          $('#latitude').val(marker.getPosition().lat());
          $('#longitude').val(marker.getPosition().lng());
        }
      }
    });
  });

});

enter code here
4

1 回答 1

0

尝试类似的东西

$('#address').trigger('change');
于 2012-01-27T09:56:31.527 回答