How to intercept when dommarker appears on the map?
I noticed that if the marker is not visible on the screen, there is not even its html code, I have to change a couple of styles, I need to know when they reappear
I use JavaScript API 3.0
This is a well-hidden feature which is documented here: http://developer.here.com/javascript-apis/documentation/v3/maps/topics_api_nlp/h-map-domicon.html#h-map-domicon
The idea is to add onAttach (and/or onDetach) callbacks to the DOM icon to be notified when a marker comes into the viewport using that icon. Watch out though because the DOM icon you create is always cloned before being put into a marker (because you can use the same DOM icon on multiple markers). The link should already show how it is done, but I'm adding a snippet below.
//Initialization business
var platform = new H.service.Platform({
app_id: 'DemoAppId01082013GAL',
app_code: 'AJKnXv84fjrb0KIHawS0Tg',
useCIT: true
});
var defaultLayers = platform.createDefaultLayers();
var map = new H.Map(document.getElementById('mapp'), defaultLayers.normal.map, {
center: {
lat: 52.3,
lng: 13.8
},
zoom: 10
});
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
//The onAttach function receives the DomElement, the icon object and the
//marker. The element can now be styled...
var onAttach = function(element, icon, marker) {
console.log("marker entering", element, icon, marker);
};
//Create a DomElement to use as marker icon...
var domIconEl = document.createElement("div");
domIconEl.style.width = "32px";
domIconEl.style.height = "32px";
domIconEl.style.marginLeft = "-16px";
domIconEl.style.marginTop = "-16px";
domIconEl.style.borderRadius = "20px";
domIconEl.style.background = "#006";
//Create icon with icon element adding onAttach to the options
var domIcon = new H.map.DomIcon(domIconEl, {
onAttach: onAttach
});
//Creare marker and add to map
var domMarker = new H.map.DomMarker({
lat: 52.3,
lng: 13.8
}, {
icon: domIcon
});
map.addObject(domMarker);
Related
I have several modal buttons which on click should show pre-saved Map Route in PolyLine.
Below code I used on php Ajax Modal Call. $jsline, $center_lat, $center_lng are php variable which are determined on modal click by ajax query.
<div id="map" style="width:100%;height:450px"></div>
<script>
$(document).on("shown.bs.modal", function () {
function addPolylineToMap(map) {
var lineString = new H.geo.LineString();
'.$jsline.'
map.addObject(new H.map.Polyline(
lineString, { style: { lineWidth: 4 }}
));
}
var platform = new H.service.Platform({
apikey: myhereapi
});
var defaultLayers = platform.createDefaultLayers();
var map = new H.Map(document.getElementById("map"),
defaultLayers.vector.normal.map,{
center: {lat:'.$center_lat.', lng:'.$center_lng.'},
zoom: 5.65,
pixelRatio: window.devicePixelRatio || 1
});
window.addEventListener("resize", () => map.getViewPort().resize());
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
var ui = H.ui.UI.createDefault(map, defaultLayers);
addPolylineToMap(map);
});
</script>
If I remove on("shown.bs.modal") modal window pops up with blank map (white background, no map). When I use on("shown.bs.modal") modal window pops up and work properly on first click. However, second, third, and further clicks will stack maps. I mean in second click there 2 maps appear. In third click 3 maps appear.
Why maps get stacked ? How to resolve this issue ? As far as I can see no one else faced with similar problem before.
I managed to find solution, so I am sharing it if anyone else faces with such issue.
I have added $("#map").empty(); just before new instance of map is loaded as below.
<div id="map" style="width:100%;height:450px"></div>
<script>
$(document).on("shown.bs.modal", function () {
function addPolylineToMap(map) {
var lineString = new H.geo.LineString();
'.$jsline.'
map.addObject(new H.map.Polyline(
lineString, { style: { lineWidth: 4 }}
));
}
$("#map").empty();
var platform = new H.service.Platform({
apikey: myhereapi
});
var defaultLayers = platform.createDefaultLayers();
var map = new H.Map(document.getElementById("map"),
defaultLayers.vector.normal.map,{
center: {lat:'.$center_lat.', lng:'.$center_lng.'},
zoom: 5.65,
pixelRatio: window.devicePixelRatio || 1
});
window.addEventListener("resize", () => map.getViewPort().resize());
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
var ui = H.ui.UI.createDefault(map, defaultLayers);
addPolylineToMap(map);
});
</script>
I would like to add a click event to (subsequently) clustered icons on a leaflet map (using the Leaflet.markercluster plugin). The event itself works, but the alert always yields the very last element of the array for every icon that is clicked. I don't see the reason. declaring 'marker' as array did not change the result.
map.clearLayers;
var marker = [];
var markers = L.markerClusterGroup({
disableClusteringAtZoom: 10,
spiderfyOnMaxZoom: true,
chunkedLoading: true
});
for (id in reclist) {
var posn = reclist[id]['info'][1];
var pose = reclist[id]['info'][2];
var title = reclist[id]['info'][0];
var mapicon = L.icon({iconUrl: 'url of icon');
marker[id] = new L.marker(new L.LatLng(posn, pose), {icon: mapicon})
.on('click', function(){alert(title)});
markers.addLayer(marker[id]);
}
map.addLayer(markers);
There are special events for that: see documentation
markers.on('clusterclick', function() {});
Note that it is one event for your cluster. So it is NOT defined in your loop.
var markers = L.markerClusterGroup({ ... });
markers.on('clusterclick', function() { ... });
for (id in reclist) { ... }
In case I misunderstood your question and you want to define a click event for each markers: if you want to display title, you cannot use the variable as you do. Should do like this:
var title = reclist[id]['info'][0];
var marker = new L.marker(new L.LatLng(posn, pose), {icon: mapicon});
marker.title = title;
marker.on('click', function(e){alert(e.target.title)});
Here is an example
I've got problem with getting zoom level of the here map.
I need to read zoom level after adding some new markers to map group.
var zoom1 = map.getZoom(); // 13
var groupTmp = new H.map.Group();
vehicle.each(function()
{
var coords = {lat: parseFloat($(this).attr('latitude')), lng: parseFloat($(this).attr('longitude'))};
groupTmp.addObject(new H.map.Marker(coords, {icon: ''}));
});
map.setViewBounds(groupTmp.getBounds());
var zoom2 = map.getZoom(); // 13
As you can see, var zoom1 and zoom2 no differs.
When I check zoom level from the console by typing "map.getZoom()" after a sec or by using setInterval, it shows correct zoom.
check the zoom after the view change has ended by adding a listener.
map.addEventListener('mapviewchangeend',handler );
var handler=function(evt) {
var zoom2 = map.getZoom();
};
I have one question,
why dose mouse events related to a map never fire when we are over marker
exp. having a mouse move listener added to a map is never called when we move over a marker
(there is not so called event propagation or bubbling).
This was actully working on maps v2!
Is it a bug, or its changed to this behavior in v3?
Blaze
Here is the example...
If you move around the map, the mapLabel is updated as should be
but if u move over the marker the mapLabel is never updated
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var myOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
google.maps.event.addListener(map, 'MOUSEMOVE', function() {
document.getElementByID('moveLabel').innerHtml = 'Mouse map move' + Math.random();
});
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"Hello World!"
});
google.maps.event.addListener(marker, 'MOUSEOUT', function() {
document.getElementByID('markerLabel').innerHtml = '';
});
google.maps.event.addListener(marker, 'MOUSEOVER', function() {
document.getElementByID('markerLabel').innerHtml = 'Mouse over marker';
});
}
All google maps' data that is drawn placed on 7 layers. These layers are called Panes. According to what pane the drawing belongs , it can receive or not receive events. For more information look at the google.maps.MapPanes documentation.
UPDATE: Google maps draws all data on 7 panes. All panes are children of the same parent. For event bubbling it is neccessary that the relation of elements should be parent-child ( then child can bubble the event to parent ),but not sibling-sibling. In sibling-sibling relation, event is received by element with the highest z-index. This is the reason you don't get events bubbled from marker's pane to map's pane.
Tried the following scripts but the last section is not working. Help please.
// script type="text/javascript"
var myOptions = {
zoom: 7,
center: new google.maps.LatLng(-36.624345125527526,144.96459973437504),
mapTypeId: google.maps.MapTypeId.TERRAIN,
streetViewControl: false,
draggableCursor: 'crosshair',
draggingCursor: 'pointer'
};
var map = new google.maps.Map(document.getElementById('map_canvas'),
myOptions);
google.maps.event.addListener(map, 'click', function(e) {
placeMarker(e.latLng, map);
});
map.panTo(position);
// not working
google.maps.event.addListener(marker, 'dragend', function() {
var point = marker.getPosition();
document.getElementById("latitude").getElementsByTagName("input")[0].value = point.lat();
document.getElementById("longitude").getElementsByTagName("input")[0].value = point.lng();
});
// end of script
Tried using the following but nothing happen after the marker is drag. Seems like the listener is not listening to the 'dragend' event of the marker. Any clue? Other option to get the latLng after dragging marker on map?
// JS code
google.maps.event.addListener(marker, 'dragend', function(e) {
var point = e.latLng;
alert(marker.e.latLng);
})
// end code
The input elements part is not a choice as the scripts sit in an embedded placeholder of a web based application. So can't change that part.
You should be using the MouseEvent object passed by the dragend event for the marker. Like this:
google.maps.event.addListener(marker, 'dragend', function(e) {
var point = e.latLng;
document.getElementById("latitude").getElementsByTagName("input")[0].value = point.lat();
document.getElementById("longitude").getElementsByTagName("input")[0].value = point.lng();
});
I also am not sure why you're not just naming your input elements and accessing them directly...something like this:
document.getElementById("latitude_input").value = point.lat();
document.getElementById("longitude_input").value = point.lng();