最近在做一个IONIC的移到开发,需添加地图;因为国外应用所以地图插件选择GOOGLE;废话不多说,下面开始上代码;
1、准备条件
angular.module('myApp', ['ngCordova', 'ionic', 'myApp.controllers'])
2、在controller.js文件 自定义Controller
.controller('GoogleMapCtrl', ['$scope', function($scope){
var mapOptions = {
zoom: 14,
mapTypeId: google.maps.MapTypeId.TERRAIN
}
$scope.map = new google.maps.Map(document.getElementById('map'), mapOptions);
$scope.geocoder =new google.maps.Geocoder();
$scope.infoWindow = new google.maps.InfoWindow();
$scope.codeAddress = function() {
$scope.geocoder.geocode({ 'address': '上海市' }, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
$scope.map.setCenter(results[0].geometry.location);
this.marker =new google.maps.Marker({
title: '上海市',
map: $scope.map,
position:results[0].geometry.location
});
$scope.infowindow =new google.maps.InfoWindow({
content: '<strong>'+ '上海市' +'</strong><br/>'+'纬度: '+ results[0].geometry.location.lat() +'<br/>经度: '+ results[0].geometry.location.lng()
});
$scope.infowindow.open($scope.map, marker);
}
});
};
}])
3、html 页面
<ion-content ng-controller="GoogleMapCtrl">
<input type="button" ng-click="codeAddress();" value="点击显示上海"/>
<div id="map" style='height:800px;width:100%;'></div>
</ion-content>
以上代码经测试,显示正确;