I am using gmaps3 to show an overlay on mouseover but the mouseover listener is not called when I move my mouse to the marker
Following is the entire code
function init_map(){
$mapElements=jQuery(".map-details");
var markers=[];
$mapElements.each(function(e){
$e=$(this);
if(!($e.data("lat")&&$e.data("long")))
return;
var img_p="https://encrypted.google.com/images/srpr/logo4w.png"
var marker= {
latLng:[$e.data("lat"),$e.data("long")],
data:{
img_preview: img_p,
properties_name:"023 Central Park [Rent]",
properties_desc:"Lorem Ipsum Go Green",
properties_link:"#",
zip:001233,
city:"Jakarta"
}
}
markers.push(marker);
});
<?php /* if($first):/**/?>
$("#map-canvas-multiple").gmap3({
map:{
// CENTRAL MAP DEFAULT
address:"New Delhi, India",
options:{
zoom:8,
scaleControl: false,
scrollwheel: false,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
}
}
},
marker:{
// DATA LOCATION
values:markers
},
events:{
mouseover: function(marker, event, context){
console.log("hello from mouseover");
$(this).gmap3(
{
clear:"overlay"
},
{
overlay:{
latLng: marker.getPosition(),
options:{
content: "<div class='info-location'>" +
"<div class='text'><h4>"
+ context.data.properties_name +
"</h4>"+
"<img src='"+ context.data.img_preview +"' width=90> $300.999 <br/>"+
context.data.properties_desc +
"<br/><a href='"+context.data.properties_link +"'class='btn btn-proper btn-small'>See Detail</a></div>" +
"</div>" +
"<div class='arrow-location'></div>",
offset: {
x:-46,
y:-73
}
}
}
});
}
}
});
}
$(document).ready(init_map);
The markers get shown but mousover on them doesn't work
No errors in console. The hello mouseover line is not printed to console either
Figured this out after many hours of brainstorming. The events object goes inside the markers object
Instead of
marker:{
// DATA LOCATION
values:markers
},
events:{
mouseover: function(marker, event, context){
console.log("hello from mouseover");
$(this).gmap3(
{
clear:"overlay"
},
it should be
marker:{
// DATA LOCATION
values:markers,
events:{
mouseover: function(marker, event, context){
console.log("hello from mouseover");
$(this).gmap3(
{
clear:"overlay"
},
},
Related
Like title says - I want to avoid map zooming after rendering directions.
I found here a lot of about it, for example Do not change map center or zoom level when rendering directions, but after adding {preserveViewport: true} to DirectionsRenderer nothing happens.
I don't want to calculate the union of the bounds of the directions responses, but only 'freeze' while rendering directions. What am I doing wrong?
My map: https://jsfiddle.net/harlowpl/xawy71r0/33/
infoWindow = new google.maps.InfoWindow();
directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer({
map: map,
preserveViewport: true,
markerOptions: {
visible: false
}
});
createSourceMarker();
createDestinationMarkers();
}
function markerClicked(destinationLocation) {
var directionsRequest = {
origin: sourceLocation,
destination: destinationLocation,
travelMode: 'DRIVING'
};
directionsService.route(directionsRequest, handleDirectionResults);
}
function handleDirectionResults(result, status) {
if (status === 'OK') {
directionsDisplay.setDirections(result);
} else {
console.log(status);
}
}
}
});
You have the zoom level set to a non-integer value. It is changing to an integer value when the directions result is displayed.
Hidden away in the documentation, it says: Specify zoom level as an integer.
proof of concept fiddle (setting zoom to 16, rather than 15.5)
code snippet:
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px;
}
<div id="map"></div>
<script>
var sourceLocation = {
lat: 52.340822,
lng: 16.855841
};
var destinationLocations = [{
lat: 52.344583,
lng: 16.849864
},
{
lat: 52.343319,
lng: 16.855080
},
];
var directionsService;
var directionsDisplay;
var infoWindow;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: 52.343580,
lng: 16.857495
},
zoom: 16,
styles: [{
"featureType": "poi",
"elementType": "all",
"stylers": [{
"visibility": "off"
}]
}],
gestureHandling: 'cooperative'
});
// console.log("zoom=" + map.getZoom());
// console.log("zoom=" + map.getZoom()+" bounds="+map.getBounds().toUrlValue());
google.maps.event.addListener(map, 'zoom_changed', function() {
console.log("zoom=" + map.getZoom());
});
google.maps.event.addListener(map, 'bounds_changed', function() {
console.log("zoom=" + map.getZoom() + " bounds=" + map.getBounds().toUrlValue());
});
infoWindow = new google.maps.InfoWindow();
directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer({
map: map,
preserveViewport: true,
markerOptions: {
visible: false
}
});
createSourceMarker();
createDestinationMarkers();
}
function markerClicked(destinationLocation) {
var directionsRequest = {
origin: sourceLocation,
destination: destinationLocation,
travelMode: 'DRIVING'
};
directionsService.route(directionsRequest, handleDirectionResults);
}
function handleDirectionResults(result, status) {
if (status === 'OK') {
directionsDisplay.setDirections(result);
} else {
console.log(status);
}
}
function createSourceMarker() {
new google.maps.Marker({
position: sourceLocation,
map: map,
icon: 'http://nakujawskiej.pl/nk/wp-content/uploads/mapMarkers/marker-main.svg'
});
}
var opis = [
'<div id="content">' +
'<div id="siteNotice">' +
'</div>' +
'<center><h4 id="firstHeading" class="firstHeading">Apteka</h4></center>' + '<hr>' +
'<div id="bodyContent">' +
'<p><b>Odległość :</b>' + ' 750m' + '<br>' + '<b>Czas dojazdu :</b>' + ' 2 min</p>' +
'</div>' +
'</div>',
'<div id="content">' +
'<div id="siteNotice">' +
'</div>' +
'<h4 id="firstHeading" class="firstHeading">Przedszkole</h4>' + '<hr>' +
'<div id="bodyContent">' +
'<p><b>Odległość :</b>' + ' 450m' + '<br>' + '<br>' + '<b>Czas dojazdu :</b>' + ' 2 min</p>' +
'</div>' +
'</div>',
];
var opisIndex = 0;
var iconBase = 'http://nakujawskiej.pl/nk/wp-content/uploads/mapMarkers/';
var markers = [
iconBase + 'marker-01.svg',
iconBase + 'marker-02.svg'
];
var markersIndex = 0;
function createDestinationMarkers() {
destinationLocations.forEach(function(location, index) {
var opisIndex = markersIndex;
var marker = new google.maps.Marker({
position: location,
map: map,
icon: markers[markersIndex++ % markers.length],
});
marker.addListener('click', function() {
infoWindow.setContent(opis[opisIndex % opis.length]);
infoWindow.open(map, marker);
});
marker.addListener('click', function() {
markerClicked(location);
});
})
}
// google.maps.event.addDomListener(window, "load", initMap);
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDo5rkrpNDFQnr5Afq9fKGmGjOTPC0C390&callback=initMap" async defer></script>
As title, I save a lot of coordinates in database, and I will get it when I click a button. I can display all markers when I click the button, but I can't draw a polyline with these markers.
Here is my code:
<div ng-app="AAA" ng-controller="Controller">
<button ng-click="SendGetPath()">Get</button>
<div class="maps">
<!-- Add directive code (gmap directive) for show map and markers-->
<ui-gmap-google-map center="map.center" zoom="map.zoom">
<ui-gmap-marker ng-repeat="marker in markers" coords="marker.coords" options="marker.options" events="marker.events" idkey="marker.id">
<ui-gmap-window options="windowOptions" show="windowOptions.show">
<div style="max-width:200px">
<div class="header"><strong>{{marker.time}}</strong></div>
</div>
</ui-gmap-window>
</ui-gmap-marker>
<ui-gmap-polyline path="map.polyline.path" stroke="map.polyline.stroke" visible='map.polyline.visible' geodesic='map.polyline.geodesic' fit="false"></ui-gmap-polyline>
</ui-gmap-google-map>
</ui-gmap-google-map>
</div>
And in javascript,
var app = angular.module('AAA', ['uiGmapgoogle-maps']);
app.controller('Controller', function ($scope, $http) {
//this is for default map focus when load first time
$scope.map = { center: { latitude: 22.25, longitude: 114.1667 }, zoom: 14, mapTypeId: google.maps.MapTypeId.ROADMAP }
$scope.carlist = [];
$scope.locations = [];
var id = 1;
//Send command to device
$scope.SendGetPath = function () {
$http.get('GetPath', {
params: {
regno: document.getElementById('car').value,
date: document.getElementById('date').value
}
}).then(function (data) {
//alert("Sent");
$scope.locations = data.data;
$scope.markers = [];
angular.forEach($scope.locations, function (value, key) {
$scope.markers.push({
id: id,
coords: { latitude: value.Latitude, longitude: value.Longitude },
//title: value.RegNo,
//battery: value.Battery
//address: data.data.Address,
//image : data.data.ImagePath
time: value.Time
});
id=id+1;
});
$scope.map = { center: { latitude: 22.25, longitude: 114.1667 }, zoom: 12 }
}, function () {
//alert('Load Error');
});
}
});
And how do I add arrow to the path of the map?
I'm trying to add a marker with a InfoWindow attached to it.
The marker is visible in both maps i.e., Street view and normal map.
But, InfoWindow only gets displayed in normal map but not when its opened in street view.
There's no error in firebug console.
The code :
var a = google.maps;
var b = {
center: new a.LatLng(parseFloat(ll[0]),parseFloat(ll[1])),
zoom: zoom,
mapTypeId: a.MapTypeId.ROADMAP,
streetViewControl: true,
mapTypeControlOptions: {
mapTypeIds: [a.MapTypeId.ROADMAP, a.MapTypeId.SATELLITE, a.MapTypeId.TERRAIN]
},
panControl: false,
zoomControlOptions: {
style: a.ZoomControlStyle.SMALL
}
};
map = new a.Map(document.getElementById("map-canvas"), b);
panorama = map.getStreetView();
panorama.setPosition(new google.maps.LatLng(42.345573,-71.098326));
panorama.setPov(/** #type {google.maps.StreetViewPov} */({
heading: 270,
pitch: 0
}));
panorama.setVisible(false);
iw = new a.InfoWindow();
a.event.addListener(map, "click", function () {
if (iw) iw.close()
});
var g = new a.Marker({
position: c,
map: map,
clickable: true,
draggable: true,
optimized: false,
raiseOnDrag: false,
zIndex: highestOrder()
});
var description = "<h2>"+document.getElementById('marker-title').value+"</h2><br/><p style='width:200px;'>"+document.getElementById('marker-desc').value+"</p>";
a.event.addListener(g, "click", function () {
actual = g;
iw.setContent(description);
if(map.getStreetView().getVisible == true) {
iw.open(map.getStreetView(), this);
}
else {
iw.open(map, this);
}
});
a.event.addListener(g, "dragstart", function () {
if (actual == g) iw.close();
z_index += 1;
g.setZIndex(highestOrder())
})
To test if the div is showing streetView imagery or a map use:
if (map.getStreetView().getVisible()) {
Not:
if(map.getStreetView().getVisible == true) {
(you aren't calling the method ...)
the click listener should be:
a.event.addListener(g, "click", function () {
actual = g;
if (map.getStreetView().getVisible()) {
iw.setContent(description);
iw.open(map.getStreetView(), this);
} else {
iw.setContent(description);
iw.open(map, this);
}
});
working example
I want to load shapes to my all_overlays array where I'm storing the shapes
drawn through DrawingManager:
drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.POLYGON,
drawingControlOptions:{
position: google.maps.ControlPosition.TOP_RIGHT,
drawingModes:[
google.maps.drawing.OverlayType.POLYGON
]
},
markerOptions: {
draggable: true
},
polylineOptions: {
editable: true
},
rectangleOptions: polyOptions,
circleOptions: polyOptions,
polygonOptions: polyOptions,
map: map
});
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {
all_overlays.push(e);
if (e.type != google.maps.drawing.OverlayType.MARKER) {
drawingManager.setDrawingMode(null);
var newShape = e.overlay;
newShape.type = e.type;
google.maps.event.addListener(newShape, 'click', function() {
setSelection(newShape);
});
setSelection(newShape);
}
});
but the problem is when I push one of this, the type of it seems to be different than the generated by drawing manager:
>all_overlays // array have a drawing manager generated object
[Object]
>coords = [ new google.maps.LatLng(-20.2164629885305, -70.1565831899643), new google.maps.LatLng(-20.2166618288932, -70.15673339366913), new google.maps.LatLng(-20.21687325381024, -70.15626400709152), new google.maps.LatLng(-20.21664672710243, -70.15616208314896)];
[Q,Q,Q,Q]
>p1= new google.maps.Polygon({paths: coords,editable: true });
Kh<br>
>all_overlays.push(p1);
2
>all_overlays;
[Object, Kh]
Then my new shape appears like 'Kh' , not like Object, then it doesn't have overlay or type properties, I'm wondering about how create a shape like the first one, created through D.M.
in order to attach to it a event listener like the another ones...
I need do that in order to can modify the loaded ones with the DM..
Thanks
You are using overlaycomplete so you have to dig a little deeper.
maybe try:
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {
all_overlays.push(e.overlay);
if (e.overlay.type != "marker") {
drawingManager.setDrawingMode(null);
var newShape = e.overlay;
newShape.type = e.overlay.type;
google.maps.event.addListener(newShape, 'click', function() {
setSelection(newShape);
});
setSelection(newShape);
}
});
to clear markers from google maps I use:
mapContent.gmap('clear', 'markers');
But what if I want to clear specific markers, not all, lets say by their IDs? Is it possible?
The way that you clear a marker from the map is by calling the setMap(null) on the marker. It looks like you are using some third party plugin (jquery-ui-map perhaps?) for google maps. If you are using jquery-ui-map, use the find method to find the marker you want, then call setMap(null) on it.
marker.setMap(null) will only clear the marker overlay. It will not destroy the marker. As "clear" is potentially for. I too want to clear the markers and destroy the one I clicked. I have a working demo which I am willing to share. Though I used marker.setMap(null) it only hides the marker. I have yet to find a way to completely destroy it.
jsFiddle:
jsfiddle.net/ryanverdel/WRyrJ/
Code:
$(document).ready(function () {
var markerCount = 0;
$("#test1").gmap3({
map: {
options: {
center: [-2.2214281090541204, -78.695068359375],
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
},
navigationControl: true,
scrollwheel: true,
disableDoubleClickZoom: true,
streetViewControl: false,
},
events: {
click: function (map, event) {
if (markerCount < 10) {
$(this).gmap3(
{
marker: {
latLng: event.latLng,
options: {
draggable: true,
animation: google.maps.Animation.DROP,
},
events: {
click: function (marker) {
marker.setMap(map);
marker.setMap(null);
marker = null;
delete marker;
console.log(marker);
markerCount--;
},
dragend: function (marker) {
$("#inputArray").empty();
setTimeout(function () {
var markers = $("#test1").gmap3({
get: {
all: true
}
});
$.each(markers, function (i, marker) {
$("#inputArray").append('<p>{"latitude":' + marker.position.lat() + ', ' + '"longitude":' + marker.position.lng() + '},' + '</p>');
});
}, 400);
}
},
},
});
markerCount++;
$("#inputArray").empty();
setTimeout(function () {
var markers = $("#test1").gmap3({
get: {
all: true
}
});
$.each(markers, function (i, marker) {
$("#inputArray").append('<p>{"latitude":' + marker.position.lat() + ', ' + '"longitude":' + marker.position.lng() + '},' + '</p>');
});
}, 400);
} else {
return false;
};
}
}
}
});
});