How do I redraw a google maps marker on click? -
i have google map set of map markers. chose draw map markers function called pinsymbol() - instead of using default image.
i want change color of pin when clicked, can't update. current code can change property of icon, can see console.log(marker), won't update color on map.
question: how redraw icon on click?
this code.
// go through restaurants , facebook info, // create marker each one. restaurants.foreach(function(restaurant){ getfacebookinfo(restaurant); }); // end foreach loop // data facebook graph api , create marker function getfacebookinfo(restaurant){ $.ajax({ url : '/restaurants/' + restaurant.id, type : 'get', datatype:'json', success : function(data) { restaurant.about = data.about; createmarker(restaurant); }, error : function(request, error) { console.log(error); alert("we're having trouble getting restaurant's info facebook. " + "please check internet connection , try refreshing page.") } }); } // create marker on map location function createmarker(restaurant){ var position = restaurant.location; var infowindow = new google.maps.infowindow({ maxwidth: 200 }); restaurant.marker = new google.maps.marker({ position: position, map: map, icon: pinsymbol('#cd212a', '#cd212a'), name: restaurant.name, id: restaurant.id, about: restaurant.about, animation: google.maps.animation.drop }); // push marker array of markers markers.push(restaurant.marker); // call populateinfowindow function populateinfowindow(restaurant.marker, infowindow); // add infowindow property restaurant // makes available use outside function. restaurant.infowindow = infowindow; this i'm stuck:
// open infowindow when marker clicked , change color restaurant.marker.addlistener('click', function(){ this.icon = pinsymbol('#eed4d9', '#eed4d9'); console.log(restaurant.marker); infowindow.open(map, this); }); } pinsymbol function
// create pin google map marker function pinsymbol(color, strokecolor) { return { path: 'm 0,0 c -2,-20 -10,-22 -10,-30 10,10 0 1,1 10,-30 c 10,-22 2,-20 0,0 z', fillcolor: color, fillopacity: 1, strokecolor: strokecolor, strokeweight: 1, scale: 1, labelorigin: new google.maps.point(0,-29) }; }
there no (documented) .icon property of marker. don't use it. use documented .seticon method:
// open infowindow when marker clicked , change color restaurant.marker.addlistener('click', function() { this.seticon(pinsymbol('#eed4d9', '#eed4d9')); console.log(restaurant.marker); infowindow.open(map, this); }); code snippet:
var geocoder; var map; var markers = []; function initialize() { 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 }); createmarker({ name: "center", id: 2, about: "", location: { lat: 37.4419, lng: -122.1419 } }); } google.maps.event.adddomlistener(window, "load", initialize); // create marker on map location function createmarker(restaurant) { var position = restaurant.location; var infowindow = new google.maps.infowindow({ maxwidth: 200 }); restaurant.marker = new google.maps.marker({ position: position, map: map, icon: pinsymbol('#cd212a', '#cd212a'), name: restaurant.name, id: restaurant.id, about: restaurant.about, animation: google.maps.animation.drop }); // push marker array of markers markers.push(restaurant.marker); // call populateinfowindow function populateinfowindow(restaurant.marker, infowindow); // add infowindow property restaurant // makes available use outside function. restaurant.infowindow = infowindow; // open infowindow when marker clicked , change color restaurant.marker.addlistener('click', function() { if (this.geticon().fillcolor != "#eed4d9") { this.seticon(pinsymbol('#eed4d9', '#eed4d9')); } else { this.seticon(pinsymbol('#cd212a', '#cd212a')); } console.log(restaurant.marker); infowindow.open(map, this); }); } // create pin google map marker function pinsymbol(color, strokecolor) { return { path: 'm 0,0 c -2,-20 -10,-22 -10,-30 10,10 0 1,1 10,-30 c 10,-22 2,-20 0,0 z', fillcolor: color, fillopacity: 1, strokecolor: strokecolor, strokeweight: 1, scale: 1, labelorigin: new google.maps.point(0, -29) }; } function populateinfowindow(marker, infowindow) { infowindow.setcontent("content"); }; 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>
Comments
Post a Comment