IONIC 添加GOOGLE地图(angularJs+googleMap)

本文介绍如何在IONIC项目中集成Google Maps插件,并通过示例代码演示如何加载地图、设置地图属性及通过地址获取经纬度并显示在地图上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在做一个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>
以上代码经测试,显示正确;




评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

戴子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值