I have a map with many routes and I would like to show all the routes in a HTML list-view below the map too and then see some interaction when I click on either the map or within the list-view. This is my code:
initialize();
$("#routes a").click(function() {
$("#debug").html("<p>I will highlight a "+ $(this).text() +"</p>");
});
function initialize() {
var myOptions = {
zoom: 16,
center: new google.maps.LatLng(10.012552, 76.327043),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
// Define an info window
var infowindow = new google.maps.InfoWindow({
content: ""
});
var highlightedPoly;
// ---------------------------------------------------------
// Encapsulate below (simulates my JSON scope)
(function () {
// Route 1 array
var polylineCoordinates = [
new google.maps.LatLng(10.013566, 76.331549),
new google.maps.LatLng(10.012552, 76.327043)
];
var polyline = new google.maps.Polyline({
title: "This is the FIRST route",
path: polylineCoordinates,
strokeColor: '#FF0000',
strokeOpacity: 2.0,
strokeWeight: 5
});
polyline.setMap(map);
// Add a "click" event for this route
google.maps.event.addListener(polyline, "click", function (e) {
// Un-highlight the current highlighted trip (if any)
if (highlightedPoly) {
highlightedPoly.setOptions({
strokeColor: "#FF0000",
zIndex: 1
});
}
// Highlight the clicked trip
polyline.setOptions({
strokeColor: "#00FFAA",
zIndex: 6000
});
// Save the trip in a global variable (needed for closure)
highlightedPoly = polyline;
infowindow.setPosition(e.latLng);
infowindow.setContent(this.title);
infowindow.open(map);
});
// Add this route to the HTML
$("#routes").append("<a href='#'>Route 1</a><br />");
})(); // close the encapsulation
// ---------------------------------------------------------
// Encapsulate below (simulates my JSON scope)
(function () {
// Route 2 array
polylineCoordinates = [
new google.maps.LatLng(10.014566, 76.331549),
new google.maps.LatLng(10.015552, 76.327043)
];
var polyline = new google.maps.Polyline({
title: "This is the SECOND route",
path: polylineCoordinates,
strokeColor: '#FF0000',
strokeOpacity: 2.0,
strokeWeight: 5
});
polyline.setMap(map);
// Add a "click" event for this route
google.maps.event.addListener(polyline, "click", function (e) {
// Un-highlight the current highlighted trip (if any)
if (highlightedPoly) {
highlightedPoly.setOptions({
strokeColor: "#FF0000",
zIndex: 1
});
}
// Highlight the clicked trip
polyline.setOptions({
strokeColor: "#00FFAA",
zIndex: 6000
});
// Save the trip in a global variable (needed for closure)
highlightedPoly = polyline;
infowindow.setPosition(e.latLng);
infowindow.setContent(this.title);
infowindow.open(map);
});
// Add this route to the HTML
$("#routes").append("<a href='#'>Route 2</a><br />");
})(); // close the encapsulation
// ---------------------------------------------------------
// Click anywhere on the map to close the info window
google.maps.event.addListener(map, "click", function () {
if (highlightedPoly) {
highlightedPoly.setOptions({
strokeColor: '#FF0000',
zIndex: 1
});
highlightedPoly = null;
}
infowindow.close();
});
}
Ideally I would like that when I hover the mouse (or click) over one of the routes in the list-view that it then would highlight the route on the map... and the opposite way, when I highlight a route on the map it should then highlight the route in the list-view:
The list-view is generated dynamically like this together with each route:
$("#routes").append("<a href='#'>Route 2</a><br />");
Can I somehow link the two together?
I have a Fiddle demo availabe.