Show/hide all figures drawing in drawingManager - javascript

My project is related with gps and tracking system. I draw geofences on google maps api v3 with drawingManager. And i need a button, clicking on which it will be show/hide all drawingManager figures. In my project i also use angularJS. I have 2 different directives. In 1-st i declare drawingManager
var polyOption = {
fillColor: '#ffff00',
fillOpacity: 0.4,
strokeWeight: 1,
clickable: true,
editable: true,
zIndex: 1
};
drawingManager = new google.maps.drawing.DrawingManager({
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.BOTTOM_CENTER,
drawingModes: [
google.maps.drawing.OverlayType.MARKER,
google.maps.drawing.OverlayType.CIRCLE,
google.maps.drawing.OverlayType.POLYGON,
google.maps.drawing.OverlayType.POLYLINE,
google.maps.drawing.OverlayType.RECTANGLE
]
},
circleOptions: polyOption,
rectangleOptions: polyOption,
polygonOptions: polyOption,
polylineOptions: polyOption,
});
scope.geozonesConfig.tempDrawManagerVar = true;
scope.geozonesConfig.tempDrawManagerHide = false;
drawingManager.setMap(map);
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {
if (e.type != google.maps.drawing.OverlayType.MARKER) {
// Switch back to non-drawing mode after drawing a shape.
drawingManager.setDrawingMode(null);
// To hide:
// Add an event listener that selects the newly-drawn shape when the user
// mouses down on it.
var newShape = e.overlay;
newShape.type = e.type;
google.maps.event.addListener(newShape, 'click', function() {
setSelection(newShape);
scope.$apply (function () {
scope.geozonesConfig.showGeozoneConfig = true;
});
});
setSelection(newShape);
});
google.maps.event.addListener(drawingManager, 'drawingmode_changed', clearSelection);
google.maps.event.addListener(map, 'click', clearSelection);
google.maps.event.addDomListener(document.getElementById('delete-button'), 'click', deleteSelectedShape);
buildColorPalette();
there is my button. i used ng-click:
<i class="icon-globe"></i>
`and my function is located into another directive:
scope.onGeozoneClick = function () {
alert('Show/Hide all figures');
};
How can i show/hide these drawingManager figures? Thanks.
EDIT: Update from OP's reply as an Answer
How can i do it in js? I dont know how to refer to my figures. And there is no any method in google maps api for these figures, as i know. Now i tried to do it using visible property of figuresOptions:
var anchorShowHideFigure = true; var polyOption = {visible = anchorShowHideFigure };
and then i want change flag to false after click on button. If i will write in my 1-st directive:
function showHideDrawingFigures () { anchorShowHideFigure = false; };
in console i get: "showHideDrawingFigures is not definied". if i will write it into 2-nd directive i get: "anchorShowHideFigure is not definied" i get this, because my html file use 2-nd directive. sorry if i explained intricately –

Checkout ng-show & ng-hide directives for this.
Create one flag say showFigures, in $scope. For toggling the fla
$scope.onGeozoneClick = function () {
$scope.showFigures = false;
};
In HTML,
<div ng-show="showFigures"></div>

Related

How to prevent feature clicks through popup?

I need a popup to be opened when clicking on the features of a vector layer. I used the Vector Icon Example as starting point. My problem is that when a feature is covered by the popup, you can still click it (Fiddle-Demo: click the lower point and you can click the upper one through the popup). How can prevent this behavior?
Relevant code:
map.on('click', function(evt) {
var element = popup.getElement();
$(element).popover({
"placement": "top",
"html": true
});
var popover = $(element).data("bs.popover");
var feature = map.forEachFeatureAtPixel(evt.pixel, function(feature, layer) {
return feature;
});
if (feature) {
popup.setPosition(feature.getGeometry().getCoordinates());
popover.options.content = feature.get("name");
$(element).popover('show');
} else {
$(element).popover('destroy');
}
});
Set stopEvent: true to your ol.Overlay object, as such:
var popup = new ol.Overlay({
element: element,
positioning: 'bottom-center',
stopEvent: true
});
See your updated Fiddle demo.

Click events not triggered in firefox (Google Maps)

This code fine work in all browser, except firefox browser.
Event click don't work.
var marker = new RichMarker({
position: new google.maps.LatLng( _latitude, _longitude ),
map: map,
draggable: draggableMarker,
content: markerContent,
flat: true
});
google.maps.event.addListener(marker, "click", function(event) {
alert(this.position);
});
How I can fix that? Thx.
The issue seems to be line 615 of http://googlemaps.github.io/js-rich-marker/src/richmarker.js
this.markerWrapper_.setCapture(true);
When you add a click-listener to a marker internally the click-event will be triggered when you click on the content of the marker. With the above line the click-event will fire for the wrapper of the content only(happens when a marker is draggable).
You'll need to modify the function addDraggingListeners_, set it to:
RichMarker.prototype.addDraggingListeners_ = function() {
var that = this;
this.draggingListeners_ = [
google.maps.event.addDomListener(window, 'mousemove', function(e) {
that.drag(e);
}, true),
google.maps.event.addDomListener(window, 'mouseup', function() {
that.stopDrag();
}, true)
];
};

Unable to add event on Google map Infobox

I am using Infobox with Google Map V3 (attached image). While clicking on Infobox, I want to go to a details page. But I am not able to add a click listener on the infobox. Here is the code I am using:
This is my infobox config:
var ib = new InfoBox({
alignBottom : true,
disableAutoPan: false,
maxWidth: 0,
pixelOffset: new google.maps.Size(-125, -50),
zIndex: null,
closeBoxURL: "",
infoBoxClearance: new google.maps.Size(1, 1),
isHidden: false,
pane: "floatPane",
enableEventPropagation:false
});
And I added listener to this infobox like this:
google.maps.event.addListener(ib, 'domready', function() {
if(Ext.get(ib.div_)){
Ext.get(ib.div_).on('click', function(){
console.log('test on click')
}, this);
Ext.get(ib.div_).on('mousedown', function(){
console.log('test on click')
}, this);
}
});
While enableEventPropagation = false, the event doesn't propagate to MAP but no event is working on the infobox.
While enableEventPropagation = true, the events (click, mousedown) works but clicking on other part of the infobox, it takes me to map or another marker.
Any idea how to solve this?
You need to add domready event to infobox's eventListener not to google maps' one. Only after infobox's html is on screen, you can bind event. To prevent multi event binding, close other infobox before you load a new one.
infobox= new InfoBox();
google.maps.event.addListener(marker, 'click', function() {
infobox.close();
infobox= new InfoBox({ ... });
infobox.open(map, this);
infobox.addListener("domready", function() {
$("#target").on("click", function(e) { /* do something */ });
});
});
You can attach a listener to whatever is the content of your InfoBox.
var boxText = document.createElement('div');
var myOptions = {
content: boxText,
...
}
var ib = new InfoBox(myOptions);
ib.open(theMap, marker);
boxText.setAttribute('onclick', 'alert("InfoBox clicked")');
Would that work for you ?

Google Map dragging Marker issue

I want to alert the user about new position on map when he/she dragged the marker. Apparently when I drag my marker, the listener even will not trigger. I want my user be able to choose to put marker on map and also be able to drag it. clicking and dragging other side of the map can change the marker position, but when I try to add the listener for dragging the map, to pop up a menu alert, it doesn't work. Thanks
var LatLng = new google.maps.LatLng(lat,lng);
var marker;
var mapOptions = {
center: LatLng,
zoom: 16,
minZoom:12,
maxZoom:18,
panControl:false,
scrollwheel: false,
rotateControl:false,
streetViewControl:false,
keyboardShortcuts:false,
mapTypeControl: false,
scaleControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var Gmap = new google.maps.Map($('#map_canvas').get(0),mapOptions);
// adding pointer by clicking on mac
google.maps.event.addListener(Gmap, 'click', function(e) {
if (marker) {
marker.setPosition(e.latLng);
}else{
marker = new google.maps.Marker({
position: e.latLng,
map: Gmap,
draggable:true
});
}
Gmap.panTo(marker.getPosition());
});
google.maps.event.addListener(marker, 'dragend', function() {
alert('Show something here');
});
The listener isn't working because when you define your listener, the marker is undefined. You need to place it in the function block that defines your marker:
google.maps.event.addListener(Gmap, 'click', function(e) {
if (marker) {
marker.setPosition(e.latLng);
} else {
marker = new google.maps.Marker({
position: e.latLng,
map: Gmap,
draggable:true
});
google.maps.event.addListener(marker, 'dragend', function() {
alert('Show something here');
});
}
Gmap.panTo(marker.getPosition());
});
See that in action right here.
could I get you to try this
google.maps.event.addListener(marker, 'dragend', function () {
updateMarkerStatus('Drag ended');
geocodePosition(marker.getPosition());
alert('Show something here');
});
You can also use firefox to put a "Break" in your javascript so that you can see if the javascript function has been triggered. info on that can be found here

google maps api v3 - open infowindow from an external click

So i have a V3 map which is initialised like this:
function init() {
var mapCenter = new google.maps.LatLng(51.5081289,-0.128005);
var map = new google.maps.Map(document.getElementById('map'), {
'zoom': 6,
'center': mapCenter,
'mapTypeId': google.maps.MapTypeId.ROADMAP,
panControl: false,
mapTypeControl: false,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL,
position: google.maps.ControlPosition.LEFT_TOP
},
});
and a load of markers that look like this:
var marker25837500 = new google.maps.Marker({
map: map,
pop_title: 'blah blah',
pop_wind: 'more blah',
zIndex: 999,
icon: 'images/map_icons/s6.png'
});
google.maps.event.addListener(marker25837500, 'click', onMarkerClick);
and lastly i have a function to open the infowindow on he click of each maker:
var infoWindow = new google.maps.InfoWindow;
var onMarkerClick = OpenInfoWindow;
function OpenInfoWindow() {
var marker = this;
infoWindow.setContent('<h3>' + marker.pop_title + '</h3>' +
marker.pop_body);
infoWindow.open(map, marker);
};
google.maps.event.addListener(map, 'click', function() {
infoWindow.close();
});
My question is, what do i need to do to make a particular marker (say marker25837500) show its infowindow when a inside the page is clicked - perhaps something like:
<div id="marker25837500">click to see infoWindow!</div>
I'm sure it's easy but i just can see my way though it!
Thanks
You might use trigger event.
$('#marker25837500').click(function () {
google.maps.event.trigger(marker25837500, 'click')
})
Check this- https://developers.google.com/maps/documentation/javascript/reference#event
Edit: Also noticed that you are calling onMarkerClick() when marker25837500 is clicked, but you named the other function OpenInfoWindow() so you might need to change that too.
You don't have to do anything unusual or simulate a click on the marker; just make sure the OpenInfoWindow function can be reached:
//This is the simple case:
var myDiv = document.getElementById( "marker25837500" );
google.maps.event.addDomListener( myDiv, "click", OpenInfoWindow );
//Or if you have other things that you want to accomplish when this occurs:
var myDiv = document.getElementById( "marker25837500" );
google.maps.event.addDomListener( myDiv, "click", function() {
OpenInfoWindow();
//Do other stuff here
});
As long as the InfoWindow is in scope (can be reached when the OpenInfoWindow function is called), this should work fine.

Categories