Open URL's from infowindows in Lightbox or similar - javascript

I have progressed in my project using Google Maps to plot a planned holiday.
The current version is
here
function initGmap() {
var map;
var clicked = false
var locations = [
['Sydney', -33.8688197, 151.2092955, 1,'https://en.wikipedia.org/wiki/Sydney'],
['Vancouver', 49.2827291, -123.1207375, 2,'https://en.wikipedia.org/wiki/Vancouver'],
['Vancouver', 49.2827291, -123.1207375, 3,'https://en.wikipedia.org/wiki/Vancouver'],
['Kamloops', 50.6745220, -120.3272674, 4,'https://en.wikipedia.org/wiki/Kamloops'],
['Jasper,Canada', 52.8736786, -118.0813581, 5,'https://en.wikipedia.org/wiki/Jasper,Canada'],
['Banff', 51.1783629, -115.5707694, 6,'https://en.wikipedia.org/wiki/Banff'],
['Lake Louise,Canada', 51.4253705, -116.1772552, 7,'https://en.wikipedia.org/wiki/Lake Louise,Canada'],
['Calgary', 51.0486151, -114.0708459, 8,'https://en.wikipedia.org/wiki/Calgary'],
['Canadian Rockies', 54.1521752, -120.1585339, 9,'https://en.wikipedia.org/wiki/Canadian Rockies'],
['Niagara Falls', 43.0962143, -79.0377388, 10,'https://en.wikipedia.org/wiki/Niagara Falls'],
['New York', 40.7127837, -74.0059413, 11,'https://en.wikipedia.org/wiki/New York'],
['Rockerfeller Building', 40.7587402, -73.9786736, 12,'https://en.wikipedia.org/wiki/Rockerfeller Building'],
['Statue Of Liberty', 40.6892494, -74.0445004, 13,'https://en.wikipedia.org/wiki/Statue Of Liberty'],
['Empire State Building', 40.7484405, -73.9856644, 14,'https://en.wikipedia.org/wiki/Empire State Building'],
['Washington D.C.', 38.9071923, -77.0368707, 15,'https://en.wikipedia.org/wiki/Washington D.C.'],
['Utah', 39.3209801, -111.0937311, 16,'https://en.wikipedia.org/wiki/Utah'],
['Arches National Park', 38.7330810, -109.5925139, 17,'https://en.wikipedia.org/wiki/Arches National Park'],
['Las Vegas', 36.1699412, -115.1398296, 18,'https://en.wikipedia.org/wiki/Las Vegas'],
['Bryce National Park', 37.5930377, -112.1870895, 19,'https://en.wikipedia.org/wiki/Bryce National Park'],
['Zion national Park', 37.2982022, -113.0263005, 20,'https://en.wikipedia.org/wiki/Zion national Park'],
['San Francisco', 37.7749295, -122.4194155, 21,'https://en.wikipedia.org/wiki/San Francisco'],
['Alcatraz', 37.8269775, -122.4229555, 22,'https://en.wikipedia.org/wiki/Alcatraz'],
['Yosemite', 37.8651011, -119.5383294, 23,'https://en.wikipedia.org/wiki/Yosemite'],
['San Francisco', 37.7749295, -122.4194155, 24,'https://en.wikipedia.org/wiki/San Francisco'],
['Sydney', -33.8688197, 151.2092955, 25,'https://en.wikipedia.org/wiki/Sydney'],
];
var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 4,
center: new google.maps.LatLng(-33.8688197, 151.2092955),
mapTypeId: google.maps.MapTypeId.ROADMAP,
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
title: locations[i][0],
map: map
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
clicked = true;
var html = '';
// Create a container for the infowindow content
html += '<div class="infowindow-content">';
// Add a link
html += 'Click for local Wiki page<br />';
html += '<locations[i][4]>' + locations[i][0] + '<br> <br>';
html+= 'Bookings<br />';
// Add an image
html += '<locations[i][5]>' + locations[i][5] + '<br>';
// Close the container
html += '</div>';
infowindow.setContent(html);
infowindow.open(map, marker);
setTimeout(function () {infowindow.close(); }, 8000);
}
})(marker, i));
google.maps.event.addListener(marker, 'mouseover', function() { if (!clicked) {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
});
google.maps.event.addListener(marker, 'mouseout', function() {
if (!clicked) {
infowindow.close();
}
});
var lineCoordinates = locations.map(function(val) {
return new google.maps.LatLng(val[1], val[2]);
});
var tripPath = new google.maps.Polyline({
path: lineCoordinates,
geodesic: true,
strokeColor: '#000',
strokeOpacity: 1.0,
strokeWeight: 2
});
tripPath.setMap(map);
}
}
initGmap();
initialize()
The issue is .. from the selected marker, pages open up in full screen.. and when I cancel out I am taken back to the first marker.
I think if I open the links in lightbox (or similar) I can just close the lightbox window..?
(Am I right ?)
So how would I add Lightbox and open the infowindow links I have in Lightbox(or similar) ?
All help or suggestions appreciated...
Cheers

Related

Remove other marker when I click on current marker

When I click on one marker then at the same time I want other marker will be deleted from goggle map. Just like I do with infowindow, when I click on one marker then only its infowindow will open.
var locations = [
[
"Location 1",
"215 West Girard Avenue 19123",
"39.9695601",
"-75.1395161"
],
[
"Location 2",
"5360 Old York Road 19141",
"40.034038",
"-75.145223"
],
[
"Location 3",
"1350 W Girard Avenue 19123",
"39.9713524",
"-75.1590360"
]
];
gmarkers = [];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: new google.maps.LatLng(39.9995601, -75.1395161),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
function createMarker(latlng, html) {
var marker = new google.maps.Marker({
position: latlng,
map: map
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(html);
infowindow.open(map, marker);
});
return marker;
}
for (var i = 0; i < locations.length; i++) {
alert(locations[i][0]);
gmarkers[locations[i][0]] =
createMarker(new google.maps.LatLng(locations[i][2], locations[i][3]), locations[i][0] + "<br>" + locations[i][1]);
}
You can put this code inside your for loop!
for (var i = 0; i < locations.length; i++) {
markerd[i] = new google.maps.Marker({
position: mark_1,
map: map,
zIndex: i
});
google.maps.event.addListener(markerd[i], 'click', function() {
for (i = 0; i < markerd.length; i++) {
if (this.index != i){
markerd[i].setMap(null);
markerp[i].setMap(null);
line[i].setMap(null);
}
}
});
}

how to show info window in google map without click event

How to show infowindow on page load in google maps. It should automatically show the infowindow pop up. I have multiple markers in the google map. I need all markers info window should open in page load.
js fiddle link
https://jsfiddle.net/vq7zfbgj/
code:
var locations = [
[
"Market 1",
22.809999,
86.179999,
5,
"Market1",
"Market1",
"Market1",
"found"
],
[
"Market 2",
22.801111,
86.171111,
5,
"Market2",
"Market2",
"Market2",
"found"
]
]
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 14,
center: new google.maps.LatLng(22.803444, 86.179525),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(locations[i][0], locations[i][6]);
infowindow.open(map, marker);
}
})(marker, i));
}
You can open multiple markers at once by initiating InfoWindow separately for each marker. I'd suggest removing the original var infowindow and doing the following within the for loop for each marker:
marker.infowindow = new google.maps.InfoWindow();
});
marker.infowindow.setContent(locations[i][0], locations[i][6]);
marker.infowindow.open(map, marker);
https://jsfiddle.net/vq7zfbgj/16/
I modified your fiddle to this and the infoWindow will open on page load. I add another marker just to ilustrate that could resolve more markers. Hope it helps.
for (i = 0; i < locations.length; i++) {
var infowindow = new google.maps.InfoWindow;
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(locations[i][0], locations[i][6]);
infowindow.open(map, marker);
}
})(marker, i));
infowindow.setContent(locations[i][0], locations[i][6]);
infowindow.open(map, marker);
}

Google maps marker - infowindow

I need to in InfoWindow show match.offer.text.
Each marker has a different match.offer.text.
This is my code:
var markerImageURL, lat, lng;
if (myoffer) {
markerImageURL = 'assets/img/markers/marker_my.png';
lat = match.lat;
lng = match.lng;
} else {
markerImageURL = 'assets/img/markers/marker_' + match.strength + '.png';
lat = match.offer.lat;
lng = match.offer.lng;
}
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
map: window.googleMap,
icon: {
size: new google.maps.Size(54,56),
url: markerImageURL
},
draggable: false,
visible: true
});
var infowindow = new google.maps.InfoWindow({
content: match.offer.text,
maxWidth: 300
});
window.googleMapMarkers.push(marker);
if(!myoffer) {
window.MVC.Events.GoogleMap.prototype.showInfoWindow(marker, infowindow, match);
}
Event triggered after clicking on the marker:
marker.addListener('click', function() {
infowindow.open(window.googleMap, marker);
}
Please, help me.
The content is applied to the marker on Open therefore your code will apply the content in last item in your loop to all markers, in your openWindow function add the content to a single infoWindow object i.e.
Also the Maps API has its own event wrapper for the click event
function initMarkers(){
//create markers
var marker = new google.maps.Marker();
google.maps.event.addListener(marker, 'click', openInfoWindow(marker, i));
}
var infowindow = new google.maps.InfoWindow();
function openInfoWindow(marker,index){
return function(e) {
//Close other info window if one is open
if (infowindow) {
infowindow.close();
}
var content = marker.offer.text;
infowindow.setContent(content);
setTimeout(function() {
infowindow.open(map, marker);
}, 200);
}
}

animated gif in array in google map

I am trying to use optimize:false parameter in my code to use animated gif as a mouseover:
var icon1 = "circle.png";
var icon2 = "circlered.gif";
var markers = [];
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
visible: false,
icon: icon1
});
google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
return function(evt) {
infowindow.setContent(locations[i][0] + "<br>" + marker.getPosition().toUrlValue(6));
infowindow.open(map, marker);
marker.setIcon(icon2);
}
})(marker, i));
markers.push(marker); // save all markers
google.maps.event.addListener(marker, 'mouseout', (function(marker) {
return function(evt) {
infowindow.close();
marker.setIcon(icon1);
}
})(marker));
when I use for example:
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
visible: false,
icon: icon2, //here I purposely changed it to animated gif first
optimize: false
});
I have no problems.
but when I try to use the same here:
google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
return function(evt) {
infowindow.setContent(locations[i][0] + "<br>" + marker.getPosition().toUrlValue(6));
infowindow.open(map, marker);
marker.setIcon(icon2);
optimize:false;// here is the wrong code obviously
}
})(marker, i));
I get the image disappearing not animating
Please suggest solution
based on the suggestions posted by the #geocoder here:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<style>
html,
body,
#map-canvas{
width: 100%;
margin: 0px;
padding: 0px;
height: 880px;
z-index:2
}
#maptwo {
z-index:1
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<!-- Include jQuery -->
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
var map;
var infowindow = new google.maps.InfoWindow();
function initialize() {
var map = new google.maps.Map(
document.getElementById("map-canvas"), {
center: new google.maps.LatLng(40.222869, 47.602673),
zoom: 13,
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var icon1 = "http://maps.google.com/mapfiles/ms/micons/blue.png";
var icon2 = {
url: "http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-35/e8nZC.gif",
scaledSize: new google.maps.Size(75, 100)
};
var markers = [];
var marker, i;
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
optimized: false,
visible: false,
icon: icon1
});
bounds.extend(marker.getPosition())
google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
return function(evt) {
infowindow.setContent(locations[i][0] + "<br>" + marker.getPosition().toUrlValue(6));
infowindow.open(map, marker);
marker.setIcon(icon2);
}
})(marker, i));
markers.push(marker); // save all markers
google.maps.event.addListener(marker, 'mouseout', (function(marker) {
return function(evt) {
infowindow.close();
marker.setIcon(icon1);
}
})(marker));
/* Change markers on zoom */
google.maps.event.addListener(map, 'zoom_changed', function() {
var zoom = map.getZoom();
// iterate over markers and call setVisible
for (i = 0; i < locations.length; i++) {
markers[i].setVisible(zoom >= 11);
}
});
}
map.fitBounds(bounds);
}
google.maps.event.addDomListener(window, "load", initialize);
var locations = [
['Location1', 39.031586, 46.590031, 5],
['Location2', 38.998439, 46.557591, 4],
['Location3', 38.913429, 46.547370, 3],
['Location4', 39.090245, 46.703794, 2],
['Location5', 39.130588, 46.696239, 1]
];
//here I create a function that will show/hide layers that are defined by the latLng values
function toggleLayer(firLayer, id) {
if ($('#' + id).is(':checked')) {
firLayer.setMap(map);
} else {
firLayer.setMap(null);
}
}
//this is the end of the function
// Fir AZE is one of the many layers that are drawn
drawAZE = new google.maps.Polygon({
path: firAZE,
strokeColor: '#000000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#00CCFF',
fillOpacity: 0.15
});
//end of FirAZE end of layer
// Add a listener for the drawAZE mouseover/mouseout event.
google.maps.event.addListener(drawAZE ,'mouseover',function(){
this.setOptions({fillColor: "#FF0000"},{fillOpacity:"0.8"});
});
google.maps.event.addListener(drawAZE ,'mouseout',function(){
this.setOptions({fillColor: "#00CCFF"},{fillOpacity:"0.5"});
});
//end of drawAZE listener
</script>
</head>
<body>
<div id="map-canvas"></div>
<div id="maptwo"></div>
<input id="fir_azerbaijan" type="checkbox" onClick="toggleLayer(drawAZE,'fir_azerbaijan')" /> AZERBAIJAN
</body>
</html>
as you can see the code in "Fir AZE" draws a layer.
then later it(the drawn layer) is supposed to be shown on click , the example provided initially at this link www.visualguide.ca/example shows that.
when I was asking about the solution for the animated marker I thought that I had problem with optimize :false - because all their lines of code were working ok.
as I have tried to implement solution provided below it worked! but I lost my ability to show/hide layers on click.sorry if this was not clear initially
my intial code was:
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(40.222869, 47.602673),
mapTypeId: google.maps.MapTypeId.TERRAIN,
zIndex: 3
};
// Set map
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
it was changed to
function initialize() {
var map = new google.maps.Map(
document.getElementById("map-canvas"), {
center: new google.maps.LatLng(40.222869, 47.602673),
zoom: 13,
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
optimized: false,
visible: false,
icon: icon1
});
bounds.extend(marker.getPosition())
//....
}
map.fitBounds(bounds);
}
I have compared line by line to locate this, and am stumbled.
One option is to create the marker with {optimized: false}:
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
optimized: false,
icon: icon1
});
google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
return function(evt) {
infowindow.setContent(locations[i][0] + "<br>" + marker.getPosition().toUrlValue(6));
infowindow.open(map, marker);
marker.setIcon(icon2);
}
})(marker, i));
markers.push(marker); // save all markers
google.maps.event.addListener(marker, 'mouseout', (function(marker) {
return function(evt) {
infowindow.close();
marker.setIcon(icon1);
}
})(marker));
}
code snippet with animated gif:
var map;
var infowindow = new google.maps.InfoWindow();
function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var icon1 = "http://maps.google.com/mapfiles/ms/micons/blue.png";
var icon2 = {
url: "http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-35/e8nZC.gif",
scaledSize: new google.maps.Size(75, 100)
};
var markers = [];
var marker, i;
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
optimized: false,
icon: icon1
});
bounds.extend(marker.getPosition())
google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
return function(evt) {
infowindow.setContent(locations[i][0] + "<br>" + marker.getPosition().toUrlValue(6));
infowindow.open(map, marker);
marker.setIcon(icon2);
}
})(marker, i));
markers.push(marker); // save all markers
google.maps.event.addListener(marker, 'mouseout', (function(marker) {
return function(evt) {
infowindow.close();
marker.setIcon(icon1);
}
})(marker));
}
map.fitBounds(bounds);
}
google.maps.event.addDomListener(window, "load", initialize);
var locations = [
['palace', 52.231871, 21.005841],
['arkadia', 52.257305, 20.984481],
['stadium', 52.215147, 21.035074]
];
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>
somehow I have managed(with the help of geocoder and Alex - thanks
//announce my variables
var icon1 = "circle.png";
var icon2 = "circlered.gif";
var markers = [];
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
optimized:false, // <-- required for animated gif
visible: false,
icon: icon1
});
I also managed to have my layers appearing on click

Change Google Maps marker icon when clicking on other

I have created a Google Maps Multiple locations page,
using Advanced Custom Fields Google Map field.
I have managed to make the marker icon change when clicked on, but I want it to be changed back when clicking on other icons.
here is an example of the code:
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
icon: iconBase + 'Stock%20Index%20Up.png'
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0], locations[i][6]);
infowindow.open(map, marker);
marker.setIcon("https://cdn3.iconfinder.com/data/icons/musthave/24/Stock%20Index%20Down.png");
}
})(marker, i));
Better look of the working code here:
http://jsfiddle.net/gargiguy/s8vgxp3g
What duncan said: What you want to do is add all your markers to an array. In your click event handler, loop over that array, updating each marker's icon. Then finally set the icon for just the marker that's been clicked.
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(locations[i][0], locations[i][6]);
infowindow.open(map, marker);
for (var j = 0; j < markers.length; j++) {
markers[j].setIcon("https://cdn3.iconfinder.com/data/icons/musthave/24/Stock%20Index%20Up.png");
}
marker.setIcon("https://cdn3.iconfinder.com/data/icons/musthave/24/Stock%20Index%20Down.png");
};
working fiddle
working code snippet:
var markers = [];
var map;
function initialize() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
// center: new google.maps.LatLng(-33.92, 151.25),
center: new google.maps.LatLng(36.8857, -76.2599),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var iconBase = 'https://cdn3.iconfinder.com/data/icons/musthave/24/';
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
icon: iconBase + 'Stock%20Index%20Up.png'
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0], locations[i][6]);
infowindow.open(map, marker);
for (var j = 0; j < markers.length; j++) {
markers[j].setIcon("https://cdn3.iconfinder.com/data/icons/musthave/24/Stock%20Index%20Up.png");
}
marker.setIcon("https://cdn3.iconfinder.com/data/icons/musthave/24/Stock%20Index%20Down.png");
};
})(marker, i));
markers.push(marker);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
var locations = [
[
"New Mermaid",
36.9079, -76.199,
1,
"Georgia Mason",
"",
"Norfolk Botanical Gardens, 6700 Azalea Garden Rd.",
"coming soon"
],
[
"1950 Fish Dish",
36.87224, -76.29518,
2,
"Terry Cox-Joseph",
"Rowena's",
"758 W. 22nd Street in front of Rowena's",
"found"
],
[
"A Rising Community",
36.95298, -76.25158,
3,
"Steven F. Morris",
"Judy Boone Realty",
"Norfolk City Library - Pretlow Branch, 9640 Granby St.",
"found"
],
[
"A School Of Fish",
36.88909, -76.26055,
4,
"Steven F. Morris",
"Sandfiddler Pawn Shop",
"5429 Tidewater Dr.",
"found"
],
[
"Aubrica the Mermaid (nee: Aubry Alexis)",
36.8618, -76.203,
5,
"Myke Irving/ Georgia Mason",
"USAVE Auto Rental",
"Virginia Auto Rental on Virginia Beach Blvd",
"found"
]
];
<script src="https://maps.google.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div>
<div id="map" style="width: 500px; height: 400px;"></div>
</div>
Since it sounds like you only need to change the previous icon back to the original, I wouldn't recommend looping through every marker. In a map with a lot of markers, this could become quite heavy.
Instead, I would store the active marker in a variable on the click event, and just update that one when it changes.
var marker;
var activeMarker;
var iconDefault = iconBase + 'Stock%20Index%20Up.png';
var iconSelected = 'https://cdn3.iconfinder.com/data/icons/musthave/24/Stock%20Index%20Down.png';
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
icon: iconDefault
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0], locations[i][6]);
infowindow.open(map, marker);
// check to see if activeMarker is set
// if so, set the icon back to the default
activeMarker && activeMarker.setIcon(iconDefault);
// set the icon for the clicked marker
marker.setIcon(iconSelected);
// update the value of activeMarker
activeMarker = marker;
}
})(marker, i));
}
You can do same like below:
var prevMarker = "";
var markers = [];
var image = { url: "your_png.png",
scaledSize: new google.maps.Size(38, 40) // If you want to resize it.
};
For creating marker,
var marker = createMarker(coordinate, map, image, id);
// coordinate = {lat:float value,long:float value} and 'map' your map
function createMarker(lat, long, map, image, marker_id) {
var coordinates = {lat: lat, lng: long};
var marker = new google.maps.Marker({
position: coordinates,
icon: image,
id: "marker_" + marker_id,
map: map
});
return marker;
}
For marker action you can use.
marker.addListener('click', function() {
console.log(this.id);
if(prevMarker !== "") {
prevMarker.setIcon({
url: "your_image.png",
scaledSize: new google.maps.Size(38, 40)
});
}
prevMarker = this;
this.setIcon({
url: "your_image.png",
scaledSize: new google.maps.Size(48, 50)
});
map.panTo(this.getPosition());
});
loop marker code for all markers available.

Categories

Resources