Google会映射如何通过按钮点击来搜索地点

时间:2013-11-22 00:12:19

标签: javascript jquery google-maps

我收到了这段代码,但是当地图加载时,我需要通过点击按钮隐藏并显示搜索结果,是否可以帮助?

代码:

      var map;
       var infowindow;

       function initialize() {
      var pyrmont = new google.maps.LatLng(-33.8665433, 151.1956316);

    map = new google.maps.Map(document.getElementById('map-canvas'), {
   center: pyrmont,
  zoom: 15
  });

  var request = {
   location: pyrmont,
   radius: 500,
    types: ['store']
 };
  infowindow = new google.maps.InfoWindow();
 var service = new google.maps.places.PlacesService(map);
  service.nearbySearch(request, callback);
 }

 function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
  createMarker(results[i]);
   }
   }
  }

function createMarker(place) {
 var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
   map: map,
   position: place.geometry.location
 });

 google.maps.event.addListener(marker, 'click', function() {
   infowindow.setContent(place.name);
   infowindow.open(map, this);
});
}

google.maps.event.addDomListener(window, 'load', initialize);\

修改

我尝试创建一个按钮,点击后显示谷歌地方的搜索结果..我隐藏了制造商,并向按钮显示标记的功能,但这不成功

             function createMarker(place) {
     var placeLoc = place.geometry.location;
      var markersr = new google.maps.Marker({
       map: map,
       icon:icon2,
       visible:false,
       position: place.geometry.location,


                     });

功能

      function showsearch() {
      markersr.setVisible(true);
       }

按钮

    <input type="checkbox"  name="btn2"  onClick="showsearch()"  class="btn2" >

1 个答案:

答案 0 :(得分:1)

请参阅链接。 http://jsfiddle.net/y829C/1/

var mapOptions = {
                  center: new google.maps.LatLng(latitude, longitude),
                  zoom: 8,
                  mapTypeId: google.maps.MapTypeId.ROADMAP

};

<强>更新

请参阅链接:http://jsfiddle.net/y829C/11/