javascript - Gmaps.js add/remove markers from array -


When a user clicks on a checkbox, I would like to show or hide some markers I am using it and The code responsible for this is:

  // Check that the user wants to display the interest dollar ("# poi"). Click (function () {var poi_markers = [], if ($ ("# poi"). (': Check')) {// Get points of interest and give them $ .jax ({type: "post ", Url:" / (data, function (key, value) {poi_marker = {marker: {lat: value.latitude, lng}: Value.longitude, icon: '/images/marker-sight.png', infoWindow: {Content: value.name}} poi_markers.push (poi_marker);}); console.log (poi_markers); map.addMarkers (poi_markers);}})}} and {map.removeMarkers (poi_markers);}}) Sample JSON:  
  [{"name": "Biskera Negra", "Latitude": "45.640981", "Longitude": "25.587723 "I get this error in firebug:" Without exception: a E Latitude or Latitude is not defined. " 

What am I doing wrong? / div>

problem # 1 addMarkers () function as a parameter marker but you can do it with marker assets They should be declared as an array of:

  poi_marker = {lat: value.latitude, lng: value.longitude, infoWindow: {content: value.name}} < / Code> 

problem # 2

removeMarkers () function does not take any parameters because It removes all the markers that should be called in such a way:

  map.removeMarkers ();  

Method of adding / removing a group of only markers

For clarity, and since I think you will understand how to do this , Then I leave the Ajax section, and assume that after collecting your all your markers are defined:

  var gMarkers = {}, actual marker = {}; [Content: "Irish Pub"}}, {Latitude: "45.645911", LNG: "25.582753", infoWindow: {Content : "Beer king"}}; [Content: "Subway"}}, {Latitude: "45.640981", LNG: "25.583723", infoWindow: {Content: "Train"}}, {lat: "45.636981", LNG: "25.580723", info window: {content: "airport"}}];  

As you can see, I've used an object gMarkers , where g Gmaps.js < / Em> because these markers are different from the actual Google Maps marker, which you will need it. Real markers will be stored in the real marker variable.

Because Gmaps.js does not provide a way to add / delete only certain markers, I have created 2 functions, which you can add to your code.

addMarkersOfType ()

  / * takes the POI type as a parameter * / GMaps.prototype.addMarkersOfType = function (Poi_type ) {// Clear markers of real markers in this way [poi_type] = []; // every Gmaps marker (i = 0 on; i & lt; gmarkers [poi_type] .length; i ++) {// marker on marker = map.addMarker (gMarkers [poi_type] [i]); // Save real markers as real markers [poi_type] Push (marker); }}  

removeMarkersOfType ()

  / * takes poi type as a parameter * / GMaps.prototype .removeMarkersOfType = Function (poi_type) {// for each of these types of actual markers (Vert I = 0; I & lt; gMarkers [poi_type] .length; i ++) {// Removal Marker realMarkers [poi_type] [I] .setMap (zero); } // Clear markers of real markers like this [poi_type] = []; }  

Example Use

  $ ("# bar_poi"). Click (function () {if ($ (this) .is (': checked')) Map.addMarkersOfType ("bar"); Other map. RemoverMarkersOfType ("bars");});  


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -