【问题标题】:Adding Autocomplete input in plugin gmap3 by jQuery通过jQuery在插件gmap3中添加自动完成输入
【发布时间】:2012-12-26 17:03:45
【问题描述】:

我想在插件 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]);
    });
});​

我该怎么办?

【问题讨论】:

    标签: javascript jquery google-maps jquery-gmap3


    【解决方案1】:

    您需要像这样在 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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-13
      • 2011-06-06
      相关资源
      最近更新 更多