Angular Leaflet map refresh - angularjs

I have a leaflet map that shows a company stores arround the country and a form that i can use to filter the stores by state, but when i change de state, i re-call the map but it doesn't refresh the markers. How can i do this? I'm using Angular Leaflet. The function that return the positions works, but the markers doesn't change. Below there is my code:
$http.get(url_sistema + 'mapaFiltro/'+estado).then(function(response) {
var local_icons = {
default_icon: {
iconUrl: url_sistema+'web_files/img/marker.png',
iconSize: [35,50],
iconAnchor: [18, 50]
}
};
angular.extend($scope, {
icons: local_icons,
center: {
lat: -20.0990815,
lng: -52.6113324,
zoom: 4
},
markers: [],
defaults: {
tileLayer: "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
zoomControlPosition: 'topright',
tileLayerOptions: {
opacity: 0.9,
detectRetina: true,
reuseTiles: true,
attribution: '© OpenStreetMap | &copy AngelLira',
},
scrollWheelZoom: false,
minZoom: 2,
worldCopyJump: true
}
});
for (var i = 0; i < response.data.dados_mapa.length; i++) {
$scope.markers.push(
{
'lat': response.data.dados_mapa[i].latitude,
'lng': response.data.dados_mapa[i].longitude,
'focus': false,
'message':
'<div class="logo_mapa">'+
'<img src="web_files/img/logo_mapa.png">'+
'</div>'+
'<div class="texto_mapa">'+
'<h1>'+response.data.dados_mapa[i].titulo+'</h1>'+
'<h2>'+response.data.dados_mapa[i].telefones+'</h2>'+
'<h2>'+response.data.dados_mapa[i].email+'</h2>'+
'<p>'+response.data.dados_mapa[i].endereco+'</p>'+
'</div>'
,
'draggable': false,
'icon': local_icons.default_icon
}
);
}
}, function(error) {
});
The response of my request are the new markers, that should me showing in the map.

Add this partial code after your for loop of push markers:
leafletData.getMap().then(function(map) {
setTimeout(function() {
map.invalidateSize();
map._resetView(map.getCenter(), map.getZoom(), true);
}, 200);
});
In additionally, you need inject leafletData to controller.

Related

how to add a rectangle on an ui-leaflet map in angular1 programmatically

in HTML my map is defnied as this
<leaflet bounds="map.spatial_bounds_card" layers="map.layers_card" height="200px" ></leaflet>
in the controller, I expand the angular scope, to define bounds and layers. I dont know how to access the leaflet map, in order to add OverlayItems to it. Currently I have created a rectangle, but its not shown on the map.
function Ctrl($scope, leafletBoundsHelpers, leafletDrawEvents) {
var drawnItems2 = new L.FeatureGroup();
angular.extend($scope, {
map: {
spatial_bounds_card: {},
layers_card: {
baselayers: {
osm: {
name: 'OpenStreetMap',
url: 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
type: 'xyz'
}
}
},
drawOptions: {
position: "bottomright",
draw: {
polyline: false,
polygon: false,
circle: false,
rectangle: {
metric: false,
showArea: true,
shapeOptions: {
color: 'blue'
}
},
marker: false
},
edit: {
featureGroup: drawnItems2,
remove: false
}
},
defaults: {
scrollWheelZoom: false
}
}
});
var bb = L.latLngBounds($scope.filters.spatial.params.southwest, $scope.filters.spatial.params.northeast);
console.log(bb);
var selectedBoundingBox = new L.rectangle(bb,
{
color: 'blue'
});
selectedBoundingBox.addTo($scope.map); // <-- error here
}
I do get this error when running the code:
angular.js:13920 TypeError: t.addLayer is not a function
at e.addTo (leaflet.js:7)
at new FilterSpatialCardCtrl (filter_spatial_card.controller.js:50)
at Object.invoke (angular.js:4718)
at $controllerInit (angular.js:10354)
at nodeLinkFn (angular.js:9263)
at angular.js:9673
at processQueue (angular.js:16383)
at angular.js:16399
at Scope.$eval (angular.js:17682)
at Scope.$digest (angular.js:17495)
In your code there is over head, extra parameters too. Make it as simple as possible.
like:
angular.extend($scope, {
center: {
lat: 54.559322,
lng: -5.767822,
zoom: 14
},
defaults: {
maxZoom: 25,
minZoom: 1,
path: {
weight: 10,
color: '#800000',
opacity: 1
}
}
});
var markersFeatureGroup = new L.FeatureGroup();
leafletData.getMap()
.then(function(map) {
var latlang = L.latLng(50.5, 30.5);
var circleobj = L.rectangle([[54.559322, -5.767822], [54.560900, -5.760000]],{
color: 'blue'
}).bindLabel("Hello World", { direction: 'bottom', clickable: "interactive" });//use left, left, auto,
markersFeatureGroup.addLayer(circleobj);
map.addLayer(markersFeatureGroup);
});
Refer Jsfiddle for more information : http://jsfiddle.net/nidhiarya1245/5hukx0g5/

Resize Google Map Marker

I want to add customized google map pin on my google map API. My image is big 135px(w) x 185px (h), i want to make it smaller , eg in 26px(w) x 35px(h) to looks fit in googlemap. Is that anyway can make the pin looks smaller besides resize the image using tools (ps/ai)?
How to add scaledSize?
var map = {
control: {},
center: {
latitude: xxx,
longitude: yyy
},
zoom: 17,
options: {
mapTypeControl: mapTypeControl,
mapTypeControlOptions: {
style: 1, // GoogleMapApi.maps.MapTypeControlStyle.HORIZONTAL_BAR
position: 1, // GoogleMapApi.maps.ControlPosition.TOP_LEFT
},
streetViewControl: false,
zoomControl: zoomControl,
zoomControlOptions: {
style: 0, // google.maps.ZoomControlStyle.DEFAULT,
position: 5, // google.maps.ControlPosition.LEFT_CENTER
},
scrollwheel: true,
panControl: false,
scaleControl: false,
draggable: true,
maxZoom: 20,
minZoom: 15,
},
trigger: false,
bounds: {},
dragging: false,
events: {
dragend: function() {
},
click: function(map, eventName, args) {
},
idle: function() {
}
},
rebuildWindows: false,
templateUrl: 'templates/infowindow.html'
};
Thank you.
If the image you want to use as a marker is too big, the first (but obvious) advice should be to change the size of your image, to fit your needs.
However, you can do it programatically :
var customPin = new google.maps.MarkerImage("../path-of-you-pin/myPin.png"),
new google.maps.Size(21, 34),
new google.maps.Point(0, 0),
new google.maps.Point(10, 34));
// or use the size you want, this are the default sizes
And use it like this :
var marker = new google.maps.Marker({
// ...
icon: customPin,
// ...
});

Angular-Leaflet-Directive: Set opacity of a layer

I am using the angular-leaflet-directive. Is there an option to change the opacity of a layer?
With 'regular' leaflet I can use layer.setOpacity(0.5). But there seems to be no option in the angular-leaflet-directive.
Edit:
here is my leaflet configuration:
angular.module('epic-taxi')
.controller('MainController', ['$scope', function($scope) {
$scope.initMap = function() {
angular.extend($scope, {
newYork: {
lat: 40.7304783951045,
lng: -73.98880004882812,
zoom: 12
},
layers: {
baselayers: {
mapbox_light: {
name: 'Light',
type: 'xyz',
url: 'http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png'
}
},
overlays: {
subway: {
name: 'Subway',
visible: true,
type: 'group',
opacity: 0.1 // not working
}
}
}
});
};
}]);
I want to change the opacity of the 'subway' overlay which contains multiple paths.
It looks like this is not implemented yet:
https://github.com/tombatossals/angular-leaflet-directive/issues/251
Thanks to Jonatas Walker for pointing it out!
Did you read their docs?
app.controller("CenterController", [ '$scope', function($scope) {
angular.extend($scope, {
center: {
lat: 40.095,
lng: -3.823,
zoom: 4,
opacity: 0.5
},
defaults: {
scrollWheelZoom: false
}
});
}]);
Try using this:
overlays: {
subway: {
name: 'Subway',
visible: true,
type: 'group',
layeroptions: {
opacity: 0.1
}
},
}

Polylines angularjs setVisible: not a boolean

I'm trying to implement polylines into my map application, I get dynamics latlng and push them into an array of polylines,but I'getting an error that I don't know why:
Error: setVisible: not a boolean
but the visible is set to true, what is wrong with it?
var x = 0;
$scope.enderecos.forEach(function (posicao) {
$scope.markers.push({
id: x,
coords: {
latitude: parseFloat(posicao.latitude),
longitude: parseFloat(posicao.longitude)
}
});
$scope.polylines.push({
id: x,
path:[{
latitude: parseFloat(posicao.latitude),
longitude: parseFloat(posicao.longitude)
}],
stroke: {
color: '#6060FB',
weight: 4
},
editable: true,
draggable: false,
geodesic: false,
visible: true
});
x++;
});
html:
<div class="angular-google-map-container">
<ui-gmap-google-map center="map.center" zoom="map.zoom" draggable="true" options="map.options" events="map.events"
control="googlemap">
<ui-gmap-window coords="MapOptions.markers.selected.coords" show="windowOptions.show" options="windowOptions"
closeClick="closeClick()">
<div>Hello</div>
</ui-gmap-window>
<ui-gmap-polylines models="polylines" path="'path'" stroke="'stroke'" visible="'visible'"
geodesic="'geodesic'" editable="'editable'" draggable="'draggable'" static="true">
</ui-gmap-polylines>
<ui-gmap-markers models="markers" idkey="markers.id" coords="'coords'" click="'onClick'"
events="markers.events"></ui-gmap-markers>
</ui-gmap-google-map>
</div>
This is my complete mapController:
.controller('MapController', function ($scope, uiGmapGoogleMapApi, $stateParams, uiGmapIsReady, ShowList) {
var viagens_id = $stateParams.viagens_id;
ShowList.enderecoViagem(viagens_id).then(function (listview) {
$scope.enderecos = listview;
$scope.MapOptions = {
minZoom: 3,
zoomControl: false,
draggable: true,
navigationControl: false,
mapTypeControl: false,
scaleControl: false,
streetViewControl: false,
disableDoubleClickZoom: false,
keyboardShortcuts: true,
markers: {
selected: {}
},
styles: [{
featureType: "poi",
elementType: "labels",
stylers: [{
visibility: "off"
}]
}, {
featureType: "transit",
elementType: "all",
stylers: [{
visibility: "off"
}]
}]
};
uiGmapGoogleMapApi.then(function (maps) {
maps.visualRefresh = true;
$scope.googlemap = {};
$scope.map = {
center: {
latitude: $scope.enderecos[0].latitude,
longitude: $scope.enderecos[0].longitude
},
zoom: 12,
pan: 1,
options: $scope.MapOptions,
control: {},
events: {
tilesloaded: function (maps, eventName, args) {
},
dragend: function (maps, eventName, args) {
},
zoom_changed: function (maps, eventName, args) {
}
}
};
});
$scope.windowOptions = {
show: false
};
$scope.onClick = function (data) {
$scope.windowOptions.show = !$scope.windowOptions.show;
console.log('$scope.windowOptions.show: ', $scope.windowOptions.show);
console.log('This is a ' + data);
//alert('This is a ' + data);
};
$scope.closeClick = function () {
$scope.windowOptions.show = false;
};
$scope.title = "Window Title!";
$scope.markers = [];
$scope.polylines = [];
var x = 0;
$scope.enderecos.forEach(function (posicao) {
$scope.markers.push({
id: x,
coords: {
latitude: parseFloat(posicao.latitude),
longitude: parseFloat(posicao.longitude)
}
});
$scope.polylines.push({
id: x,
path: [{
latitude: parseFloat(posicao.latitude),
longitude: parseFloat(posicao.longitude)
}],
stroke: {
color: '#6060FB',
weight: 4
},
editable: true,
draggable: false,
geodesic: true,
visible: true
});
x++;
});
$scope.addMarkerClickFunction = function (markersArray) {
angular.forEach(markersArray, function (value, key) {
value.onClick = function () {
$scope.onClick(value.data);
$scope.MapOptions.markers.selected = value;
};
});
};
uiGmapIsReady.promise() // if no value is put in promise() it defaults to promise(1)
.then(function (instances) {
console.log(instances[0].map); // get the current map
})
.then(function () {
$scope.addMarkerClickFunction($scope.markers);
});
});
})
I think that your error is in this line:
<ui-gmap-polylines models="polylines" path="'path'" stroke="'stroke'" visible="'visible'"geodesic="'geodesic'" editable="'editable'" draggable="'draggable'" static="true">
</ui-gmap-polylines>
You put the variable "visible" between two simple quotes " ' ". This transforms the variable visible into the String visible. You are evaluating the string 'visible', not the boolean true
Try this:
<ui-gmap-polylines models="polylines" path="path" stroke="stroke" visible="visible"geodesic="geodesic" editable="editable" draggable="draggable" static="true">
</ui-gmap-polylines>

geolocation + distance measurement

launch: function() {
// The following is accomplished with the Google Map API
var position = new google.maps.LatLng(12.9833, 77.5833), //Sencha HQ
infowindow = new google.maps.InfoWindow({
content: 'bengaluru'
}),
//Tracking Marker Image
image = new google.maps.MarkerImage(
'resources/images/point.png',
new google.maps.Size(32, 31),
new google.maps.Point(0, 0),
new google.maps.Point(16, 31)
),
shadow = new google.maps.MarkerImage(
'resources/images/shadow.png',
new google.maps.Size(64, 52),
new google.maps.Point(0, 0),
new google.maps.Point(-5, 42)
),
trackingButton = Ext.create('Ext.Button', {
iconMask: true,
iconCls: 'locate'
}),
trafficButton = Ext.create('Ext.Button', {
iconMask: true,
pressed: true,
iconCls: 'maps'
}),
toolbar = Ext.create('Ext.Toolbar', {
docked: 'top',
ui: 'light',
defaults: {
iconMask: true
},
items: [
{
xtype:'button',
id:'calculate',
text:'calculate',
handler:function(){
alert('hello');
}
}
]
});
var mapdemo = Ext.create('Ext.Map', {
mapOptions : {
center : new google.maps.LatLng(37.381592, 122.135672), //nearby San Fran
zoom : 12,
mapTypeId : google.maps.MapTypeId.ROADMAP,
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.DEFAULT
}
},
plugins : [
new Ext.plugin.google.Tracker({
trackSuspended: true, //suspend tracking initially
allowHighAccuracy: false,
marker: new google.maps.Marker({
position: position,
title: 'My Current Location',
shadow: shadow,
icon: image
})
}),
new Ext.plugin.google.Traffic()
],
listeners: {
maprender: function(comp, map) {
var marker = new google.maps.Marker({
position: position,
title : 'Sencha HQ',
map: map
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
setTimeout(function() {
map.panTo(position);
}, 1000);
}
}
});
Any ideas how can i measure distance between two destinations whose latitude and longitude are know........
this code is taken from the example provided by the sencha touch2 with little modification.
Its for sencha touch 2
try google.maps.geometry.spherical.computeDistanceBetween(from:LatLng, to:LatLng, radius?:number)
radius is the earth's radius = 6371000 metres (average)

Resources