【问题标题】:Google Map API: Searching through text in infowindow possible?Google Map API:可以在信息窗口中搜索文本吗?
【发布时间】:2012-06-22 15:50:41
【问题描述】:

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

我想做的是让用户通过表单在 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();

【问题讨论】:

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


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

    【讨论】:

    • 谢谢。不确定在当前代码中的确切位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多