App.service('Map', function($window, $document, $q) {
var promise;
this.load = function() {
if (promise) {
return promise;
}
promise = $q(function(resolve, reject) {
$window.initMap = function() {
resolve();
return;
};
var script = document.createElement("script");
script.type = "text/javascript";
script.src="http://webapi.amap.com/maps?v=1.4.0&key=5ce80b5c333d681c6d63&callback=initMap";
$document[0].body.appendChild(script);
});
return promise;
};
});
//控制器代码如下
//地图
$scope.openMapModal = function (type) {
var modalInstance = $modal.open({
templateUrl: '/myModalContent.html',
controller: ModalInstanceCtrl,
size: 'lg',
scope: $scope
});
$scope.gpsType=type;
};
var ModalInstanceCtrl = function ($scope, $modalInstance,$timeout) {
$timeout(function(){
Map.load().then(function(){
var map = new AMap.Map("container", {
resizeEnable: true,
center: [116.40, 39.91],//地图中心点
zoom: 13 //地图显示的缩放级别
});
// 为地图注册click事件获取鼠标点击出的经纬度坐标
var clickEventListener = map.on('click', function(e) {
if($scope.gpsType ==1){
$scope.gpsx=e.lnglat.getLat();
$scope.gpsy=e.lnglat.getLng();
console.log( $scope.gpsx + ", " + $scope.gpsy)
}else if($scope.gpsType ==2){
$scope.rallyGpsX=e.lnglat.getLat();
$scope.rallyGpsY=e.lnglat.getLng();
console.log( $scope.rallyGpsX + ", " + $scope.rallyGpsY)
}
});
//这里是搜索
AMap.plugin('AMap.Autocomplete',function(){//回调函数
var autoOptions = {
city: "", //城市,默认全国
input:"tipinput"//使用联想输入的input的id
};
var autocomplete= new AMap.Autocomplete(autoOptions);
AMap.event.addListener(autocomplete, "select", function(data){
//TODO 选择后的处理程序,data的格式见 附录
});
});
var autoOptions = {
input: "tipinput"
};
var auto = new AMap.Autocomplete(autoOptions);
var placeSearch = new AMap.PlaceSearch({
map: map
}); //构造地点查询类
AMap.event.addListener(auto, "select", select);//注册监听,当选中某条记录时会触发
function select(e) {
placeSearch.setCity(e.poi.adcode);
placeSearch.search(e.poi.name); //关键字查询查询
}
});
},100);
$scope.ok = function () {
$modalInstance.close('确定');
if($scope.gpsType ==1){
$scope.formData.gpsx= $scope.gpsx;
$scope.formData.gpsy= $scope.gpsy;
}else if($scope.gpsType ==2){
$scope.formData.rallyGpsX= $scope.rallyGpsX;
$scope.formData.rallyGpsY= $scope.rallyGpsY;
}
};
$scope.cancel = function () {
$modalInstance.dismiss('取消');
};
};
一样的代码按照api单独写个demo可以实现,但是放在项目中就没有反应,求解???
有这么几种可能
1.路径问题
2.看console控制台,静态文件是不是被拦截器拦截了,以至于不能访问到