Link to home
Start Free TrialLog in
Avatar of Refael
RefaelFlag for United States of America

asked on

MapBox Icon SVG size on hover/click

Hello Experts,
I hope somone can help.... I am using MapBox for one of my projects with 3 layers for each POI type. Each POI layer using a custom icon SVG I uploaded via the MapBox Studio.
I am showing a popup when the user click on each POI and the popup is position fixed on the Map.
Currently the size of the icon SVG is set at 0.75. I need somehow to increase the size of the icon or replace the icon (what ever is easier) when the user click or mouseover each POI.
Below is my current script:


mapboxgl.accessToken = 'xyz'; 
 
	 var geojson = {
		type: 'FeatureCollection',
		features: [{
			type: 'Feature',
				geometry: {
					type: 'Point',
					coordinates: [19.225290,56.551136]
				},
		}]
	 };

		var map = new mapboxgl.Map({
			container: 'map',
			style: 'mapbox://styles/xyz/cjk4c9eaj2tv22rqp316keuaq',
			center: [19.225290,56.545136], // starting position [lng, lat]
			zoom: 13.5,
			scrollZoom: false,
			pitch: 10,
			rotation:10,
		});


var lastZoom = map.getZoom();

map.on('zoom', function() {
  const currentZoom = map.getZoom();
  if (currentZoom < 13 || currentZoom > 14) {
    $(".marker").hide();
  } else {
     $(".marker").show();
  }

  lastZoom = currentZoom;
});


map.on('load', function() {
	
	var layers = ['slf-urban', 'slf-land', 'slf-park'];

	var layer = $('#map-filters input[type="checkbox"]').on("change", function(c) {
		
		var thislayer = $(this).attr("id");
		
		if(thislayer == "slf-all") {
			for (var i = 0; i < layers.length; i++) {
		  	map.setLayoutProperty(layers[i], "visibility", "visible");
				$("#map-filters li").not(".slf-all").removeClass("selected");
				$("#map-filters li.slf-all").addClass("selected");
			}
		
		} else {	
			for (var i = 0; i < layers.length; i++) {
			map.setLayoutProperty(layers[i], "visibility", "none");
				$("#map-filters li").removeClass("selected");
				var thisInput = $(this).parent();
				$(thisInput).addClass("selected");
				map.setLayoutProperty(thislayer, "visibility", "visible");
			}
	
		}
	});
  
	// add markers to map
	geojson.features.forEach(function(marker) {
		// create a HTML element for each feature
		var el = document.createElement('div');
		el.className = 'marker';
		// make a marker for each feature and add to the map
		new mapboxgl.Marker(el)
		.setLngLat(marker.geometry.coordinates)  
		.addTo(map);
	}); 
});





var layersId = ['slf-urban', 'slf-land', 'slf-park'];

for (var i = 0; i < layersId.length; i++) {
    var layerId = layersId[i];

	map.on('click', layerId, function (e) {
		$(".mapboxgl-popup").hide();
		var coordinates = e.features[0].geometry.coordinates.slice();
		var address = e.features[0].properties.address;
		var category = e.features[0].properties.category;
		var description = e.features[0].properties.description;
		var name = e.features[0].properties.place_name;
		var photo = e.features[0].properties.photo;

		// Ensure that if the map is zoomed out such that multiple
		// copies of the feature are visible, the popup appears
		// over the copy being pointed to.
		while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
				coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
		}

		new mapboxgl.Popup({closeOnClick: true, closeButton: true})
			.setLngLat(coordinates)
			.setHTML('<div class="lagepop '+category+'"><h1>'+name+'</h1><p>'+description+'</p><p>'+address+'</p><div class="location-photo"><img src="'+photo+'"></div></div>')		 
			.addTo(map);		
		  $(".mapboxgl-popup").addClass("show");
	});
}

map.doubleClickZoom.disable();
map.addControl(new mapboxgl.FullscreenControl());
// Add zoom and rotation controls to the map.
map.addControl(new mapboxgl.NavigationControl({
	// Hide rotation control.
	showCompass: false
}), 'top-right');

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

Avatar of Refael

ASKER

Hi leakim97111 :-)
I already read the documentation on mapbox yet have not yes found what I am after. Or did yo find there a solution?
if I understand you, you want to do something like this (the big one) :

User generated image
Avatar of Refael

ASKER

Hi leakim971, Thank you!

It is very simple. I already have the map with custom icons (poi's). the only think i am searching of is how to integrate a mouse-over effect such as increasing the icons size or replacing the icon e.g. referencing a new icon (svg) on mouse over. Since I use the mapbox studio together with the script i have attached in my initial post i have no clue how to do it. I hope i clarify the question?
If it look like the screenshot I posted, you can use jQuery to find the icon and change the URL on mouse over

User generated image
Avatar of Refael

ASKER

leakim971 hi
I thought you said you do not really know mapbox? This script you sent is using leaflet js library. it is not really helpful, sorry.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.