Google Map API:在infowindow中搜索文本可能吗?

时间:2012-06-22 15:50:42

标签: google-maps google-maps-api-3

我目前正在开发一个应用程序,根据用户的帖子,在Google地图上放置各种标记和infowindows。我还包括地理编码,以便用户可以更改其位置并查看任何区域中的标记/帖子。

我想要做的是让用户通过表单搜索infowindows中的文本信息,然后地图通过显示包含该文本窗口的标记进行响应。我已经搜索了API并且我没有看到提到的这种能力,虽然看起来它应该是可以实现的。

非常感谢有关如何实现这一目标的任何见解或信息。

这是应用程序中的当前代码:

function mainGeo()
{
     if (navigator.geolocation) 
        {
          navigator.geolocation.getCurrentPosition( mainMap, error, {maximumAge: 30000, timeout: 10000, enableHighAccuracy: true} );
    }
    else
    {
          alert("Sorry, but it looks like your browser does not support geolocation.");
    }
}


var stories = {{storyJson|safe}};
var geocoder;
var map;


function loadMarkers(stories){
    for (i=0;i<stories.length;i++) {
        var story = stories[i];

        (function(story) {
            var pinColor = "69f2ff";
                var pinImage = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=S|" + pinColor,
                    new google.maps.Size(21, 34),
                    new google.maps.Point(0,0),
                    new google.maps.Point(10, 34));
          var point = new google.maps.LatLng(story.latitude, story.longitude);
          var marker = new google.maps.Marker({position: point, map: map, icon: pinImage});
          var infowindow = new google.maps.InfoWindow({
            content: '<div >'+
                '<div >'+
                '</div>'+
                '<h2 class="firstHeading">'+story.headline+'</h2>'+
                '<div>'+
                '<p>'+story.author+'</p>'+
                '<p>'+story.city+'</p>'+
                '<p>'+story.topic+'</p>'+
                '<p>'+story.date+'</p>'+
                '<p>'+story.copy+'</p>'+
                '<p><a href='+story.url+'>Click to read story</a></p>'+
                '</div>'+
                '</div>'

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



 function mainMap(position)
 {
       geocoder = new google.maps.Geocoder();
       // Define the coordinates as a Google Maps LatLng Object
       var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);


       // Prepare the map options
       var mapOptions =
      {
                  zoom: 15,
                  center: coords,
                  mapTypeControl: false,
                  navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                  mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        // Create the map, and place it in the map_canvas div
        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

        // Place the initial marker
        var marker = new google.maps.Marker({
                  position: coords,
                  map: map,
                  title: "Your current location!"
        });

        loadMarkers(stories);

    }


  function codeAddress() {
    var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }


function error() {
    alert("You have refused to display your location. You will not be able to submit stories.");
    }

mainGeo();

1 个答案:

答案 0 :(得分:0)

  1. 创建三个空数组(例如markersinfowindowsmatches
  2. 在您实例化标记时,通过markers数组中的索引引用标记(例如,markers[i] = marker
  3. 在实例化infowindow时,通过infowindows数组中的索引(例如,infowindows[i] = htmltext [或您存储内容的任何变量名称]引用它的内容
  4. infowindows数组中搜索字符串,将包含该字符串的项的索引存储在matches数组中,然后使用带有matches数组的for循环添加markers数组中的标记(基于matches数组的索引值)。