Google Maps API自动完成

时间:2012-02-25 03:41:10

标签: jquery api map autocomplete google-maps-api-3

我正在尝试使用Google Maps API创建两个自动完成输入框以及用于标记位置的地图。

目前基于文档中的示例我有一个单独的输入框,可以使用自动完成并在地图上放置一个标记,但是我现在想要扩展它并且有第二个输入框。 相同地图上的相同内容,但无法实现它。

到目前为止的代码是

<script type="text/javascript">
        function initialize() {
                var mapOptions = {
                  center: new google.maps.LatLng(-33.8688, 151.2195),
                  zoom: 13,
                  mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map(document.getElementById('map'),
                  mapOptions);

                var input = document.getElementById('frombox');
                var autocomplete = new google.maps.places.Autocomplete(input);

                autocomplete.bindTo('bounds', map);

                var infowindow = new google.maps.InfoWindow();
                var marker = new google.maps.Marker({
                  map: map
                });


                google.maps.event.addListener(autocomplete, 'place_changed', function() {
                  infowindow.close();
                  var place = autocomplete.getPlace();
                  if (place.geometry.viewport) {
                    map.fitBounds(place.geometry.viewport);
                  } else {
                    map.setCenter(place.geometry.location);
                    map.setZoom(15);
                  }


                  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(map, marker);

                });



                // Sets a listener on a radio button to change the filter type on Places
                // Autocomplete.
                function setupClickListener(id, types) {
                  var radioButton = document.getElementById(id);
                  google.maps.event.addDomListener(radioButton, 'click', function() {
                    autocomplete.setTypes(types);
                  });
                }

                setupClickListener('changetype-all', []);
                setupClickListener('changetype-establishment', ['establishment']);
                setupClickListener('changetype-geocode', ['geocode']);
              }
              google.maps.event.addDomListener(window, 'load', initialize);                 

                        </script>

任何人都可以帮我设置第二个名为#tobox的输入框,在同一张地图上做同样的事情,我真的很感激。

由于

0 个答案:

没有答案