通过jQuery在插件gmap3中添加自动完成输入

时间:2012-12-26 17:03:45

标签: javascript jquery google-maps jquery-gmap3

我想在插件gmap3中添加自动完成输入,点击地址后移动标记就可以获得经度和纬度,但我无法完成。我试着:

演示: http://jsfiddle.net/ezJUm/

<div id="content">
    <input id="searchTextField" type="text">
    <div id="map_canvas" class="line"></div>
    <div id="latlng" class="line">click the map</div>
    <div id="address" class="line">click the map</div>
</div>

$(document).ready(function () {
    // create the map
    var map = $("#map_canvas").gmap3({
        lat: 43.0566,
        lng: -89.4511,
        zoom: 12
    });
    //*********************** Autocomplete *********************************
    var lat = 26.535266981346876,
        lng = 54.02773082256317,
        latlng = new google.maps.LatLng(lat, lng),
        image = 'http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png';

    mapG = new google.maps.Map(document.getElementById('map_canvas')),
    marker = new google.maps.Marker({
        position: latlng,
        map: mapG,
        icon: image
    });
    var input = document.getElementById('searchTextField');
    var autocomplete = new google.maps.places.Autocomplete(input, {
        types: ["geocode"]
    });
    autocomplete.bindTo('bounds', mapG);
    var infowindow = new google.maps.InfoWindow();
    //*********************** Autocomplete *********************************

    // add click handlers
    map.onclickReverseGeocode(function (address) {
        $("#address").html(address);
    });
    map.onclickGetLatLng(function (latlng) {
        $("#latlng").html(latlng[0] + ',' + latlng[1]);
    });
});​

我该怎么办?

1 个答案:

答案 0 :(得分:5)

您需要在autoComplete对象上设置place_changed事件侦听器,如下所示。 Here是自动填充的一个示例。

google.maps.event.addListener(autocomplete, 'place_changed', function() {
      infowindow.close();
      marker.setVisible(false);
      input.className = '';
      var place = autocomplete.getPlace();
        console.log(place);
      if (!place.geometry) {
        // Inform the user that the place was not found and return.
        input.className = 'notfound';
        return;
      }

      // If the place has a geometry, then present it on a map.
      if (place.geometry.viewport) {
        mapG.fitBounds(place.geometry.viewport);
      } else {
        mapG.setCenter(place.geometry.location);
        mapG.setZoom(17);  // Why 17? Because it looks good.
      }
      var image = new google.maps.MarkerImage(
          place.icon,
          new google.maps.Size(71, 71),
          new google.maps.Point(0, 0),
          new google.maps.Point(17, 34),
          new google.maps.Size(35, 35));
      marker.setIcon(image);
      marker.setPosition(place.geometry.location);

      var address = '';
      if (place.address_components) {
        address = [
          (place.address_components[0] && place.address_components[0].short_name || ''),
          (place.address_components[1] && place.address_components[1].short_name || ''),
          (place.address_components[2] && place.address_components[2].short_name || '')
        ].join(' ');
      }

      infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
      infowindow.open(mapG, marker);
    });

我已经更新了jsFiddle - http://jsfiddle.net/ezJUm/1/