We can define the url of a custom icon which will be used in order to display the marker.
<!DOCTYPE html>
<html>
<head>
<style>
#map {
height: 400px;
width: 600px;
}
</style>
</head>
<body>
<h3>My Google Maps Demo</h3>
<div id="map"></div>
<script>
function initMap() {
var location = { lat: 35.339186, lng: 25.1315903 };
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: location
});
var image = 'https://maps.google.com/mapfiles/kml/shapes/library_maps.png';
var marker = new google.maps.Marker({
position: location,
map: map,
icon: image
});
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>
</body>
</html>
The following example demonstrates how to reset the marker's icon to the default icon when the user clicks on the marker.
<!DOCTYPE html>
<html>
<head>
<style>
#map {
height: 400px;
width: 600px;
}
</style>
</head>
<body>
<h3>My Google Maps Demo</h3>
<div id="map"></div>
<script>
function initMap() {
var location = { lat: 35.339186, lng: 25.1315903 };
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: location
});
var image = 'https://maps.google.com/mapfiles/kml/shapes/library_maps.png';
var marker = new google.maps.Marker({
position: location,
map: map,
icon: image
});
google.maps.event.addListener(marker, 'click', function () {
marker.setIcon(null);
});
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>
</body>
</html>
If we want to define a different icon, we can replace the null value with the url of the icon that we want to use.
<!DOCTYPE html>
<html>
<head>
<style>
#map {
height: 400px;
width: 600px;
}
</style>
</head>
<body>
<h3>My Google Maps Demo</h3>
<div id="map"></div>
<script>
function initMap() {
var location = { lat: 35.339186, lng: 25.1315903 };
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: location
});
var image = 'https://maps.google.com/mapfiles/kml/shapes/library_maps.png';
var marker = new google.maps.Marker({
position: location,
map: map,
icon: image
});
google.maps.event.addListener(marker, 'click', function () {
var image = 'https://maps.google.com/mapfiles/kml/shapes/parking_lot_maps.png';
marker.setIcon(image);
});
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>
</body>
</html>
When we change the icon of the marker, we can define other values as well in order to define the behaviour of the icon.
<!DOCTYPE html>
<html>
<head>
<style>
#map {
height: 400px;
width: 600px;
}
</style>
</head>
<body>
<h3>My Google Maps Demo</h3>
<div id="map"></div>
<script>
function initMap() {
var location = { lat: 35.339186, lng: 25.1315903 };
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: location
});
var image = 'https://maps.google.com/mapfiles/kml/shapes/library_maps.png';
var marker = new google.maps.Marker({
position: location,
map: map,
icon: image
});
google.maps.event.addListener(marker, 'click', function () {
var image = new google.maps.MarkerImage(
"https://maps.google.com/mapfiles/kml/shapes/parking_lot_maps.png", // url
new google.maps.Size(32, 37), // size
new google.maps.Point(0, 0), // origin
new google.maps.Point(16, 37), // anchor
new google.maps.Size(32, 37)); // scaledSize
marker.setIcon(image);
});
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>
</body>
</html>