How to avoid Heatmap layers in google maps stacking over each other - google-maps-api-3

I am trying to implement a HTML slider and attaching the slider value with the radius of the location's heatmap. As I increase the slider , the radius is increasing properly. But as I reduce it, instead of reducing, it is overlaying on the existing heatmap, Hence could not see any difference.
HTML Code
<div class="slidecontainer">
<style="text-align:center; font: 5px; padding-top: 0px;"> Choose the Time range </style>
<br><input type="range" min="1" max="24" value="0" class="slider" name="sder" form="form1" id="myRange" onchange="updateTextInput(this.value);"></div>
JS Code
var chenai = new google.maps.LatLng(13.051126, 80.195590);
map = new google.maps.Map(document.getElementById('map'), {
center: chenai,
zoom: 13,
mapTypeId: 'satellite'
});
function updateTextInput(val) {
var testheatMapData = [
{location: new google.maps.LatLng(13.034603, 80.207509), weight: 20},
{location: new google.maps.LatLng(13.071126, 80.195590), weight: 15},
{location: new google.maps.LatLng(13.025914, 80.156675), weight: 10} ];
heatmap = new google.maps.visualization.HeatmapLayer({
data: heatMapData,
radius: val,
});
heatmap.setMap(map);
}

Create the heatmap layer and
Update its radius in the update function called by the slider
var map;
var heatmap;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(37.783, -122.445),
zoom: 13
};
map = new google.maps.Map(document.getElementById('map'), mapOptions);
var heatmapData = [
new google.maps.LatLng(37.782, -122.447),
new google.maps.LatLng(37.782, -122.445),
new google.maps.LatLng(37.782, -122.443),
new google.maps.LatLng(37.782, -122.441),
new google.maps.LatLng(37.782, -122.439),
new google.maps.LatLng(37.782, -122.437),
new google.maps.LatLng(37.782, -122.435),
new google.maps.LatLng(37.785, -122.447),
new google.maps.LatLng(37.785, -122.445),
new google.maps.LatLng(37.785, -122.443),
new google.maps.LatLng(37.785, -122.441),
new google.maps.LatLng(37.785, -122.439),
new google.maps.LatLng(37.785, -122.437),
new google.maps.LatLng(37.785, -122.435)
];
heatmap = new google.maps.visualization.HeatmapLayer({
data: heatmapData,
radius: 10,
map: map
});
}
function updateTextInput(val) {
heatmap.setOptions({
radius: val
});
}
#map {
height: 170px;
}
<div id="map"></div>
<input type="range" min="1" max="24" value="10" class="slider" name="sder" form="form1" id="myRange" onchange="updateTextInput(this.value);">
<!-- Replace the value of the key parameter with your own API key. -->
<script src="//maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initialize&libraries=visualization" async defer></script>

Related

Reset markers icon in google maps v3 Javascript

I am changing the icon for the current selected marker with something like this:
var icon_active = {
url: '/2018/images/map/location.png',
size: new google.maps.Size(32, 32),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(16, 42)
};
markers[window.VARS.marker_currently_open].setIcon(icon_active);
The problem I'm having, is how to reset it back to what it was before? I've tried to find a list of the standard icons, ie:
Could anyone advise me on how to do this? What I was trying to find was a list of the different (default) icons that are available, and find the standard one?
To set the default icon, call marker.setIcon(null).
i.e.:
markers[window.VARS.marker_currently_open].setIcon(null);
proof of concept fiddle
code snippet:
var markers = [];
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 marker = new google.maps.Marker({
position: map.getCenter(),
map: map
})
markers.push(marker);
window.VARS = {
marker_currently_open: 0
}
var icon_active = {
url: 'http://maps.google.com/mapfiles/ms/micons/blue.png',
size: new google.maps.Size(32, 32),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(16, 42)
};
markers[window.VARS.marker_currently_open].setIcon(icon_active);
setTimeout(function() {
markers[window.VARS.marker_currently_open].setIcon(null);
}, 5000);
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>

How to get true centerpoint of offset marker

I don't think this has been asked before - I can't find any answers to this through searching SO and Google, and I find this sort of odd so maybe I'm overlooking something..
Anyways, I am creating a pretty standard series of markers based on lat/lng coordinates, although very often the markers will "stack" on top of each other, since they are at the same lat/lng coordinates. We aren't looking to cluster the markers when this happens, we are offsetting them instead so they appear side by side.
The problem I'm having though, is when I call marker.getPosition to get the lat/lng object of the marker's position, it isn't taking into account the offset. It's giving the original lat/lng coordinates of the centerpoint of where the marker was placed before it was offset.
Is there a way I can find the TRUE centerpoint of a marker, taking its offset into account? I know what the offset is in pixels, if that helps.
Here's a code snippet of what I'm doing
var marker = new google.maps.Marker({
map: obj.map,
position: latlng,
icon: {
url: obj.icon,
size: new google.maps.Size(72, 72),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(obj.offset_x, obj.offset_y),
scaledSize: new google.maps.Size(25, 25)
},
title: obj.title,
zIndex: obj.zIndex
});
var latlng = marker.getPosition();
So when I do
var lat = latlng.lat();
var lng = latlng.lng();
it seems like I get the position of the marker, and not the offset position of the icon that represents the marker. Does anyone know how I can get the offset position of the icon as a latlng object?
You can compute the position:
overlay = new google.maps.OverlayView();
overlay.draw = function() {};
overlay.setMap(map);
var markerPt = overlay.getProjection().fromLatLngToDivPixel(marker.getPosition());
var point = new google.maps.Point(markerPt.x - obj.offset_x + 12.5, markerPt.y - obj.offset_y + 25);
proof of concept fiddle
code snippet:
var map;
function initialize() {
map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP,
});
overlay = new google.maps.OverlayView();
overlay.draw = function() {};
overlay.setMap(map);
var obj = {
map: map,
icon: "http://maps.google.com/mapfiles/ms/micons/blue.png",
offset_x: 100,
offset_y: 50,
title: "marker",
zIndex: 0
}
var latlng = {
lat: 37.4419,
lng: -122.1419
};
var marker = new google.maps.Marker({
map: obj.map,
position: latlng,
icon: {
url: obj.icon,
size: new google.maps.Size(72, 72),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(obj.offset_x, obj.offset_y),
scaledSize: new google.maps.Size(25, 25)
},
title: obj.title,
zIndex: obj.zIndex
});
var center = new google.maps.Marker({
map: map,
position: latlng,
icon: "http://maps.google.com/mapfiles/ms/micons/green.png",
title: "center",
draggable: true
});
google.maps.event.addListener(overlay, "projection_changed", function() {
// if (!overlay.getProjection()) return;
var markerPt = overlay.getProjection().fromLatLngToDivPixel(marker.getPosition());
console.log("markerPt=" + markerPt.toString());
// extra offset to bottom middle of marker (12.5, 25)
var point = new google.maps.Point(markerPt.x - obj.offset_x + 12.5, markerPt.y - obj.offset_y + 25);
console.log("point=" + point.toString());
var latLng = overlay.getProjection().fromDivPixelToLatLng(point);
console.log("latLng=" + latLng.toUrlValue(6));
var newMark = new google.maps.Marker({
map: map,
position: latLng,
icon: {
url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle.png",
size: new google.maps.Size(7, 7),
anchor: new google.maps.Point(3.5, 3.5)
}
});
var infowindow = new google.maps.InfoWindow();
infowindow.setContent(newMark.getPosition().toUrlValue(6));
infowindow.setOptions({
pixelOffset: new google.maps.Size(0, -25)
});
infowindow.setPosition(newMark.getPosition());
infowindow.open(map);
});
var latlng = marker.getPosition();
console.log("latlng=" + latlng.toUrlValue(6));
var infowindow = new google.maps.InfoWindow();
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>

Is it possible to have the editable attribute turned off in a polyline but still display the vertices as clickable circles?

I would like to display a polyline so that the vertices can not be moved, deleted or added, ie exactly like when the editable attribute is set to false, but the circles which are present when the editable attribute is set to true are still visible so that they can be clicked and a vertex number obtained.
So the polyline code could be:
newPoly = new google.maps.Polyline({
strokeColor: '#08088a',
strokeWeight: 2,
editable: false
});
Is this possible?
One option: process through the polyline, add circular markers to each vertex in the line with the vertex number in the marker's infowindow.
Related question: Google Maps V3 Polyline : make it editable without center point(s)
proof of concept fiddle
code snippet:
function initialize() {
var infowindow = new google.maps.InfoWindow();
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 polyCoord = [
new google.maps.LatLng(41.86, 8.73),
new google.maps.LatLng(41.88, 8.75),
new google.maps.LatLng(42, 8),
new google.maps.LatLng(43.5, 9)
];
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < polyCoord.length; i++) {
bounds.extend(polyCoord[i]);
var marker = new google.maps.Marker({
position: polyCoord[i],
title: '#0',
map: map,
icon: {
path: google.maps.SymbolPath.CIRCLE,
fillColor: 'white',
fillOpacity: 1,
scale: 3,
strokeColor: 'black',
strokeWeight: 1,
strokeOpacity: 1,
// anchor: new google.maps.Point(200, 200)
}
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent("vertex #" + i + "<br>coord: (" + this.getPosition().toUrlValue(6) + ")");
infowindow.open(map, marker);
}
})(marker, i));
}
map.fitBounds(bounds);
// Polyline
var newPoly = new google.maps.Polyline({
strokeColor: '#08088a',
strokeWeight: 2,
editable: false,
path: polyCoord,
map: map
});
}
google.maps.event.addDomListener(window, "load", initialize);
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" style="border: 2px solid #3872ac;"></div>

svg static image within the area of a polygon

How do I get the svg image is static when I zoom in on the map, which always remains in the same place.
This is my fiddle
If I use png images works , but it is not visually well for me and is not what i'm looking for.
Help is appreciated
Sorry for my english.
new Fiddle
The anchor is expected to be a Point, not a LatLng.
The default-acnchor is the bottom-middle of the icon, as it seems you need to set it to the top-left, so it has to be:
new google.maps.Point(0,0)
When you want to have a scaled icon based on the zoom you must calculate the scale-property and re-assign the icon to the marker.
The formula would be(assuming the scale-factor at zoom 12 is 1):
Math.pow(2,map.getZoom()-12)
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-32.95041520, -60.66641804),
zoom: 12,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
triangleCoords = [
new google.maps.LatLng(-32.93831432, -60.69379806),
new google.maps.LatLng(-32.96337859, -60.67860603),
new google.maps.LatLng(-32.96352262, -60.66633224),
new google.maps.LatLng(-32.95041520, -60.66641807)
];
var bermudaTriangle = new google.maps.Polygon({
paths: triangleCoords,
IsInactivo: true
});
bermudaTriangle.setMap(map);
var bounds = new google.maps.LatLngBounds();
var i;
for (i = 0; i < triangleCoords.length; i++) {
bounds.extend(triangleCoords[i]);
}
console.log(bounds.getCenter());
centroPolygon = bounds.getCenter();
var inactive = new google.maps.MVCObject();
inactive.set('icon', {
path: 'M27.314 4.686c-3.022-3.022-7.040-4.686-11.314-4.686s-8.292 1.664-11.314 4.686c-3.022 3.022-4.686 7.040-4.686 11.314s1.664 8.292 4.686 11.314c3.022 3.022 7.040 4.686 11.314 4.686s8.292-1.664 11.314-4.686c3.022-3.022 4.686-7.040 4.686-11.314s-1.664-8.292-4.686-11.314zM28 16c0 2.588-0.824 4.987-2.222 6.949l-16.727-16.727c1.962-1.399 4.361-2.222 6.949-2.222 6.617 0 12 5.383 12 12zM4 16c0-2.588 0.824-4.987 2.222-6.949l16.727 16.727c-1.962 1.399-4.361 2.222-6.949 2.222-6.617 0-12-5.383-12-12z',
fillColor: '#FF5858',
fillOpacity: 0.4,
scale: 1,
strokeColor: '#FF5858',
strokeWeight: 1,
//set the anchor to the top left corner of the svg
anchor: new google.maps.Point(0, 0)
});
google.maps.event.addListener(map, 'zoom_changed', function() {
inactive.get('icon').scale = Math.pow(2, this.getZoom() - 12);
//tell the marker that the icon has changed
inactive.notify('icon');
});
google.maps.event.trigger(map, 'zoom_changed');
new google.maps.Marker({
map: map,
position: centroPolygon
}).bindTo('icon', inactive, 'icon');
}
google.maps.event.addDomListener(window, 'load', initialize)
html,
body,
#map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?v=3&.js"></script>
<div id="map-canvas"></div>

The correct way to hide a polyline?

I´ve a function that show a polyline on a map, this part is working, now I want to implement a function that hides the polyline, but I can´t find my mistake, thanks in advance.
function cargaMapaCYL(mapa, varControl){
var limite = null;
limite = [
new google.maps.LatLng(42.49956716,-7.019005501),
new google.maps.LatLng(42.49947126,-7.029286373),
new google.maps.LatLng(42.50904062,-7.049299123),
new google.maps.LatLng(42.50722622,-7.069103626),
new google.maps.LatLng(42.50452387,-7.000150672),
new google.maps.LatLng(42.49348015,-6.983058917),
new google.maps.LatLng(42.49843269,-6.971666546),
new google.maps.LatLng(42.51765791,-6.956909023),
new google.maps.LatLng(42.52010069,-6.927429186),
new google.maps.LatLng(42.50992238,-6.914231493),
new google.maps.LatLng(42.50096695,-6.879679821),
new google.maps.LatLng(42.48775868,-6.857775832),
new google.maps.LatLng(43.23907504,-3.293216584)], "#000000", 5);
var contorno= new google.maps.Polyline({
path: limite,
strokeColor: "#000000",
strokeOpacity: 1.0,
strokeWeight: 2
});
if(varControl==true){
contorno.setMap(mapa);
}
if(varControl==false){
contorno.setMap(null);
}
}
You only need to create the Polyline once. Put it into a global var contorno = ... Then you can create a toggle function using the setVisible(boolean) method.
if(contorno.getVisible()){
contorno.setVisible(false);
else{
contorno.setVisible(true);
}
// or
contorno.getVisible() ? contorno.setVisible(false) : contorno.setVisible(true);
Blow is an example which hides the path after 3 seconds.
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<script>
// This example creates a 2-pixel-wide red polyline showing the path of William
// Kingsford Smith's first trans-Pacific flight between Oakland, CA, and
// Brisbane, Australia.
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: {lat: 0, lng: -180},
mapTypeId: 'terrain'
});
var flightPlanCoordinates = [
{lat: 37.772, lng: -122.214},
{lat: 21.291, lng: -157.821},
{lat: -18.142, lng: 178.431},
{lat: -27.467, lng: 153.027}
];
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
setTimeout(function() {
alert('hide path');
flightPath.setVisible(false);
}, 3000);
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>
Everytime your function is called, it creates a new polyline. Which is either added to the map or not.
Persumably you want be able to call the function once with true to add the line, then again with false to remove it. At the moment, when you call it a second time, its creates a new line and doesn't add it to the map. It does not touch the original line, already added to the map.
One way is too keep the line in a global variable. Then you can refer to the exact same object between calls.
var contorno = null;
function cargaMapaCYL(mapa, varControl){
if (!contorno) {
var limite = [...], "#000000", 5);
contorno= new google.maps.Polyline({...});
}
if(varControl){
contorno.setMap(mapa);
} else {
contorno.setMap(null);
}
}

Resources