最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to find a certain marker on Google Map? - Stack Overflow

programmeradmin1浏览0评论

I have a table with search results and an array of markers for the results. Every element has an ID, and if a user clicks on a marker, the page scrolls to the search result (I'm just changing window.location.hash).

Is it possible to do it vice-versa? Users click on the search result and the infoWindow for a marker appears.

I have a table with search results and an array of markers for the results. Every element has an ID, and if a user clicks on a marker, the page scrolls to the search result (I'm just changing window.location.hash).

Is it possible to do it vice-versa? Users click on the search result and the infoWindow for a marker appears.

Share Improve this question edited Jul 3, 2011 at 12:35 Trott 70.3k27 gold badges182 silver badges217 bronze badges asked Jul 3, 2011 at 9:17 Daniel J FDaniel J F 1,0642 gold badges16 silver badges31 bronze badges 1
  • "if a user clicks on a marker, the page scrolls to the search result " can you help me for this ? – Kanagasabapathy Rajkumar Commented Mar 14, 2014 at 10:37
Add a ment  | 

1 Answer 1

Reset to default 3

I assume you mean that in the map the marker associated to the search result scrolls into view?

As long as your search result has a click event that contains some unique identifier for the marker you want to appear, sure it will work. A site I'm currently managing has a sidebar of search results where when you click the result it will scroll the marker into view on the map and show the infowindow. The code invoked by the click handler looks something like so (sans some error handling and other uninteresting parts for your question):

function MarkerZoomTo(markerIdentifier) {
    pt = gMarkers[markerIdentifier].getPosition();
    newpt = new google.maps.LatLng(pt.lat() + .02, pt.lng());
    map.panTo(newpt);

    if (infoWindow) {
    infoWindow.close();
    }

    infoWindow.setContent(gMarkers[markerIdentifier].get('iwcontent'));
    infoWindow.setPosition(gMarkers[markerIdentifier].getPosition());

    infoWindow.open(map, gMarkers[markerIdentifier]);
}

I pass the unique marker identifier to the function, get the specific marker out of my marker array, get it's lat/lng position, create a LatLng object, and pan to that location.

The rest is just dressing to retrieve the content for the infowindow and set it's position which you could do any way you see fit.

And finally, just open the infowindow for the marker.

Good luck!

发布评论

评论列表(0)

  1. 暂无评论