AngularJS Leaflet getMap() doesn't work - angularjs

After adding Leaflet to my AngularJS app:
<leaflet id="map" defaults="defaults" center="center" bounds="bounds" controls="controls" event-broadcast="events"></leaflet>
And setting it up:
// Initialise the feature group to store editable layers
var drawnItems = new L.FeatureGroup();
// Initialise the draw control
var drawControl = new L.Control.Draw({
position: 'topright',
edit: {
featureGroup: drawnItems
}
});
// Configure Leaflet
angular.extend($scope, {
defaults: {
zoomControlPosition: 'topright',
minZoom: 3,
tileLayerOptions: {
detectRetina: true,
reuseTiles: true,
attribution: 'OpenStreetMaps'
}
},
center: {},
controls: {
custom: [drawControl]
},
events: {
map: {
enable: ['click']
}
}
});
Following this code it doesn't get evaluated (no error shown though):
leafletData.getMap().then(
function (map) {
alert('I have accessed the map.');
}
);
This should show me an alert straight away, although nothing happens.
If I delay this previous code, for example, running it in a function on a button click, it works!
Does anyone knows what could be a problem?
Seeing example, it should work: https://github.com/tombatossals/angular-leaflet-directive/blob/master/examples/control-draw-example.html
PARTLY SOLVED
Removing ID from leaflet HTML tag solved the problem. Must be a bug.

You are supposed to pass the id specified in the <leaflet> tag to getMap() function.
In your example, the id is map. You would pass it like this:
leafletData.getMap("map").then(
function (map) {
alert('I have accessed the map.');
}
);

Related

ScrollTo: #div tag instead of top of page

I am trying to use the ScrollTo function in this little snippet of code so my page scrolls to an ID div tag instead of the top of page "0". Any help would be much appreciated!
Here is the code I'm using to scroll to the top of the page. All works fine just need to figure out how to add in a div location rather than it just going to the top.
I should also mention I am using smoothstate.js
Thanks so much for the help :)
$(function(){
'use strict';
var $body = $('html,body, #smoothState');
var options = {
prefetch: true,
cacheLength: 2,
blacklist: ".no-smoothstate a, .post-edit-link, a[href*='.jpg'], a[href*='.png'], a[href*='.jpeg'], a[href*='.pdf']",
onStart: {
duration: 0, // Duration of our animation
render: function ($container) {
$('.spinner').fadeIn(0);
// Add your CSS animation reversing class
$container.addClass('is-exiting');
$body.animate({
scrollTop: 0
});
// Restart your animation
smoothState.restartCSSAnimations();
}
},
onReady: {
duration: 0,
render: function ($container, $newContent) {
$('.spinner').fadeOut(0);
// Remove your CSS animation reversing class
$container.removeClass('is-exiting');
// Inject the new content
$container.html($newContent);
}
},
Here is how I am doing it now. Seems to be working fine. Hope this helps out some others that are not so pro at jQuery like myself haha :)
$('html, body').animate({
scrollTop: $("#target-element").offset().top
}, 1000);

Angular Leaflet Directive Not updating marker position

I'm trying to use angular-leaflet-directive with Websocket, though I'm able to integrate successfully, the positions of the markers are not getting updated dynamically. However The positions of map gets updated if I move mouse over the map but doesn't get updated when the lat-lng value changes.
Below is code snippet of module.
$scope.markers = {};
angular.extend($scope, {
bounds : $scope.bounds,
center : {},
kppaths : {},
events: {
markers:{
enable: [ 'move' ]
}
}
});
$stomp.setDebug(function(args) {
$log.info(args);
});
$scope.startWS = function() {
var connectionHeaders = {};
// Connect
$stomp.connect("/kp-ws").then(function(frame){
$log.info("connected to server")
$stomp.send('/app/start', {});
// Subscribe for message
$scope.subscription = $stomp.subscribe('/topic/kp', function(
data, headers, res) {
angular.forEach(data, function(k,v){
$scope.markers[k.markerId].lat = k.lat;
$scope.markers[k.markerId].lng = k.lng;
});
});
});
};
$scope.stopWS = function() {
$stomp.send('/app/stop', {});
$scope.subscription.unsubscribe();
$stomp.disconnect();
};
$scope.$on("leafletDirectiveMarker.move", function(event, args){
$log.info(args.model.lat);
});
} ]);
The html file
<div class="card-block">
<leaflet bounds="bounds" geojson="geojson" lf-center="center"
paths="kppaths" markers="markers" event-broadcast="events" width="100%" height="480px"></leaflet>
</div>
Is I'm missing something, Please let me know or suggest how to fix this issue?
The possible workaround I found is:
leafletData.getMap().then(function (map) {
$timeout(function() {map.invalidateSize()});
});
basically, once the map is invalidated, it updates markers' position. Although not perfect, considering some performance issues, the workaround at least solves the main issue.

How to add info window for custom overlays?

I have implemented a custom overlay. I want to add an event which will pop an info window when I click the overlay.
function initMap() {
var map = new google.maps.Map(document.getElementById('okmap'), {
zoom: 4,
center: {lat: 35.0000, lng: 103.0000}
});
for (var n in nodes) {
var myMarker = new MyMarker(map, {latlng: new google.maps.LatLng(nodes[n][0], nodes[n][1]),
image: 'assets/img/light-green.png',
labelText: (Math.random() * 100).toFixed(0)});
markers.push(myMarker);
var infowindow = new google.maps.InfoWindow({
content: n
});
myMarker.addListener('click', function() {
infowindow.open(map, myMarker);
});
}
}
But it is not working!
I faced a simliar problem some time back. Here are two things that helped me.
I am considering the main overlay element to be a div.
In your MyMarker.draw function make sure that you are using a pane that has access to the DOM events.e.g.
var panes = this.getPanes();
panes.overlayMouseTarget.appendChild(div);
See https://developers.google.com/maps/documentation/javascript/customoverlays#initialize for more details.
Make sure you define an event listener, also in the MyMarker.draw function.
google.maps.event.addDomListener(div, "click", function(event) {
google.maps.event.trigger(self, "click");
});

Access InfoWindow from angular google map

I am using google maps via angularJS (using angular-google-map)
I have a marker with an InfoWindow which works fine except on iOS. When I rotate the device, the google map API doesn't seem to calculate the size well and the infoWindow is too small for its content. I reckon this is the same bug as this one
So I have been trying to access the InfoWindow object to close it and open it again to force google to redraw/recalculate but couldn't find a way to access it.
I can access the google.maps.Map object by using $scope.map.control.getGMap() in angular but can't access the InfoWindow. I can see a getGWindows in Window.prototype but no idea how to access it from the map. I can access the markers as well but they don't have a reference to their infowindow.
Here is the way I create the marker in case it can be of any help:
var marker = {
id: obj.id,
name: obj.name,
latitude: obj.lat,
longitude: obj.lng,
address: obj.address,
suburbCity: obj.suburbCity,
state: obj.state,
postcodeZip: obj.postcodeZip,
icon: "xxx.png"),
options: {
labelContent: obj.name,
labelClass: "none",
labelAnchor:"25 0",
opacity: 0.25,
animation: google.maps.Animation.DROP,
visible: true
},
templateurl:'markerwindow.tpl.html',
templateparameter: data,
closeClick: function() {
marker.options.visible = false;
return $scope.$apply();
},
onClicked: function() {
navigateToMarkerOnMap(marker);
}
};
$scope.map.markers.push(marker);
function navigateToMarkerOnMap(marker) {
$scope.selected.options.visible = false;
$scope.selected = marker;
$scope.selected.options.visible = true;
}

Dynamically changing Leaflet map bounds in Angular?

I'm using Angular-leaflet-directive. I have a menu where the user taps an option. Each time the user clicks an option the map is updated with two new markers and the bounds should be adjusted so these markers are both visible. The markers get updated but the bounds seem to work differently. The first time the user clicks something the bounds are updated, but they don't update for each time after that. This is the code which is called each time the user selects an option:
var updateMap = function() {
setDirections();
$scope.bounds = leafletBoundsHelpers.createBoundsFromArray([
[$scope.thisPractice.end.k, $scope.thisPractice.end.D],
[$scope.thisPractice.start.k, $scope.thisPractice.start.D]
]);
$scope.markers = {
end: {
title: "Destination",
lat: $scope.thisPractice.end.k,
lng: $scope.thisPractice.end.D,
focus: true,
icon: local_icons.markerRed
},
start: {
title: "Start",
lat: $scope.thisPractice.start.k,
lng: $scope.thisPractice.start.D,
}
}
}
This is my map initialization code:
// Initialize the map
angular.extend($scope, {
center: {},
bounds: {},
paths: {},
markers: {},
scrollWheelZoom: false,
layers: {
baselayers: {
googleRoadmap: {
name: 'Google Streets',
layerType: 'ROADMAP',
type: 'google'
}
}
}
});
This is the HTML where my map occurs:
<div id="map_canvas">
<leaflet id="leaflet_map" bounds="bounds" center="center" markers="markers" paths="paths" height="300px" width="100%" layers="layers"></leaflet>
</div>
It's not an issue with my data since the markers are updated fine. Any ideas? It seems weird that the bounds wouldn't function in the same way as the markers do.
Edit:
So the following code utilises the standard Leafleat.js functions and works:
var bounds = L.latLngBounds([$scope.thisPractice.end.k, $scope.thisPractice.end.D], [$scope.thisPractice.start.k, $scope.thisPractice.start.D])
leafletData.getMap().then(function(map) {
map.fitBounds(bounds);
});
It would be nice if the angular directive implemented fitbounds in the same way.

Resources