[英]Add Marker on Google map wherever I click
I want to achieve something similar to this http://jsfiddle.net/sya8gn0w/1/ . 我想实现类似于此http://jsfiddle.net/sya8gn0w/1/的功能 。
Now the problem is I have my own custom directive to display map. 现在的问题是我有自己的自定义指令来显示地图。 I want a function in child controller which will achieve above mentioned functionality on some button click.
我想要子控制器中的某个功能,该功能将在单击某些按钮时实现上述功能。
eg. 例如。 The place marker functionality triggers only if I click on some button.
仅当我单击某些按钮时,才会触发位置标记功能。
Present code - 当前代码-
function ParentCtrl($scope){
var mainCtrl = this;
angular.extend(mainCtrl , {
map: {
center: {
latitude: 18.5,
longitude: 73.85
},
zoom: 13,
markers: [],
events: {
click: function (map, eventName, originalEventArgs) {
var e = originalEventArgs[0];
var lat = e.latLng.lat(), lon = e.latLng.lng();
var marker = {
id: Date.now(),
coords: {
latitude: lat,
longitude: lon
}
};
mainCtrl .map.markers.pop();
mainCtrl .map.markers.push(marker);
console.log(mainCtrl .map.markers);
console.log("latitude : "+lat+" longitude : "+lon);
$scope.$apply();
}
}
}
});
};
I want to move the functionality present in 'click:' part of angular.extend to a function presents in ChildCtrl controller. 我想将angular.extend的'click:'部分中存在的功能移动到ChildCtrl控制器中存在的功能。 Is it possible ?
可能吗 ? Ortherwise suggest me different approach to achieve this.
另外,建议我以其他方式实现这一目标。
I don't know whether my answer going to help you or not. 我不知道我的答案是否会对您有所帮助。 Recently I worked on GoogleMap, I want to share what I know.
最近,我在GoogleMap上工作,我想分享我所知道的。
angular.extend(scope, {
map: {
show: true,
control: {},
version: 'uknown',
center: {
latitude: 0,
longitude: 0
},
options: {
streetViewControl: false,
panControl: false,
maxZoom: 10,
minZoom: 1
},
zoom: 2,
dragging: false,
bounds: {},
markers: scope.markers,
doClusterMarkers: true,
currentClusterType: 'standard',
clusterTypes: clusterTypes,
selectedClusterTypes: selectedClusterTypes,
clusterOptions: selectedClusterTypes['standard'],
events: {
tilesloaded: function(map, eventName, originalEventArgs) {},
click: function(mapModel, eventName, originalEventArgs) {},
dragend: function() {}
},
toggleColor: function(color) {
if (color === 'red') {
return '#6060FB';
} else {
return 'red';
}
}
}
Here the statement markers: scope.markers
will help to create new markers or to remove old markers. 在这里,声明
markers: scope.markers
将有助于创建新的标记或删除旧的标记。
In "ChildCtrl" you write a function to update scope.markers object of parent controller. 在“ ChildCtrl”中,您可以编写一个函数来更新父控制器的scope.markers对象。
You could declare the function for adding a marker in a separate controller and then utilize $controller
service to inject that controller into another controller: 您可以声明在单独的控制器中添加标记的函数,然后利用
$controller
服务将该控制器注入另一个控制器:
angular.module('main', ['uiGmapgoogle-maps'])
.controller('MapCtrl', function ($scope,$controller) {
$controller('MarkerCtrl', {$scope: $scope});
angular.extend($scope, {
map: {
center: {
latitude: 42.3349940452867,
longitude:-71.0353168884369
},
zoom: 11,
markers: [],
events: {
click: function (map, eventName, originalEventArgs) {
var e = originalEventArgs[0];
$scope.addMarker(e.latLng);
$scope.$apply();
}
}
}
});
})
.controller('MarkerCtrl', function($scope) {
angular.extend($scope, {
addMarker: function(latLng) {
var marker = {
id: Date.now(),
coords: {
latitude: latLng.lat(),
longitude: latLng.lng()
}
};
$scope.map.markers.push(marker);
}
});
});
I used different but better approach to achieve the desired results. 我使用了不同但更好的方法来达到期望的结果。 Found this solution on fiddle .
在小提琴上找到了这个解决方案。 Hope this would be helpful for someone else also.
希望这对其他人也有帮助。
function dragMarkerService(resolveAddressService){
this.placeMarker = function(location, idValue, map){
var myMarker = new google.maps.Marker({
position: new google.maps.LatLng(location.latitude, location.longitude),
draggable: true
});
google.maps.event.addListener(myMarker, 'dragend', function (evt) {
location.latitude = evt.latLng.lat();
location.longitude = evt.latLng.lng();
resolveAddressService.resolveAddress(location, idValue)
});
google.maps.event.addListener(myMarker, 'dragstart', function (evt) {
angular.element(idValue).val() = 'Currently dragging marker...';
});
map.setCenter(myMarker.position);
myMarker.setMap(map);
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.