【发布时间】:2016-09-05 06:29:22
【问题描述】:
我想在我的页面中显示带有可拖动标记的 Google 地图。这是我的代码:
header.php:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&sensor=true"></script>
map.js:
app.directive('mapDirective',function(){
return {
templateUrl: 'map.html',
restrict: 'EA',
require: '?ngModel',
scope:{
myModel: '=ngModel'
},
link: function(scope , element, attrs , ngModel){
var mapOptions;
var googleMap;
var searchMarker;
var searchLatLng;
scope.searchLocation = {
latitude: 48.137273,
longitude: 11.575251
};
ngModel.$render = function(){
console.log("hhh");
searchLatLng = new google.maps.LatLng(scope.myModel.latitude, scope.myModel.longitude);
mapOptions = {
center: searchLatLng,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
googleMap = new google.maps.Map(element[0],mapOptions);
searchMarker = new google.maps.Marker({
position: searchLatLng,
map: googleMap,
draggable: true
});
google.maps.event.addListener(searchMarker, 'dragend', function(){
scope.$apply(function(){
scope.myModel.latitude = searchMarker.getPosition().lat();
scope.myModel.longitude = searchMarker.getPosition().lng();
});
}.bind(this));
};
scope.$watch('myModel', function(value){
var myPosition = new google.maps.LatLng(scope.myModel.latitude, scope.myModel.longitude);
searchMarker.setPosition(myPosition);
}, true);
}
}
});
map.html:
<div>
<div style="display: block; height: 200px; width: 100%; ">
</div>
</div>
index.html:
<map-directive ng-model="testModel"></map-directive>
实际上,我得到了这个错误:
TypeError: Cannot read property 'latitude' of undefined
如何解决?
已编辑: 我更改了我的 maps.js:
app.directive('mapDirective',function(){
return {
templateUrl: '/app/user/ngApp/templates/libsView/templates/directives/map.html',
restrict: 'EA',
require: '?ngModel',
scope:{
myModel: '=ngModel'
},
controller: function ($scope) {
$scope.searchLocation = {
latitude: 48.137273,
longitude: 11.575251
};
},
link: function(scope , element, attrs , ngModel){
var mapOptions;
var googleMap;
var searchMarker;
var searchLatLng;
ngModel.$render = function(){
console.log("hhh");
searchLatLng = new google.maps.LatLng(scope.myModel.latitude, scope.myModel.longitude);
mapOptions = {
center: searchLatLng,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
googleMap = new google.maps.Map(element[0],mapOptions);
searchMarker = new google.maps.Marker({
position: searchLatLng,
map: googleMap,
draggable: true
});
google.maps.event.addListener(searchMarker, 'dragend', function(){
scope.$apply(function(){
scope.myModel.latitude = searchMarker.getPosition().lat();
scope.myModel.longitude = searchMarker.getPosition().lng();
});
}.bind(this));
};
scope.$watch('myModel', function(value){
var myPosition = new google.maps.LatLng(scope.myModel.latitude, scope.myModel.longitude);
searchMarker.setPosition(myPosition);
}, true);
}
}
});
但它不会改变。
【问题讨论】:
-
请提供您的控制器代码\
-
testModel 包含什么?
-
@HardikPatel 什么都没有。我应该添加什么?
-
myModel 变量应该包含纬度和经度值。应该从
设置 -
@HardikPatel 请把它作为答案
标签: html angularjs google-maps google-maps-api-3 google-maps-markers