在SAP WEB IDE简单测试基于HTML5自带的定位功能,相关步骤如下:
1.VIEW代码
1 <mvc:View xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m" 2 controllerName="zrico_appnszrico_pn_scan.controller.GeoLocation" xmlns:html="http://www.w3.org/1999/xhtml"> 3 <App> 4 <pages> 5 <Page 6 title="{i18n>GeoLocation.Page.title}" 7 showNavButton="true" 8 navButtonPress="onNavBack" 9 backgroundDesign="Transparent"> 10 <content> 11 <Button id="BTGeoLoc" text="{i18n>GeoLocation.Button.text}" type="Emphasized" press="onGeoLocButton" > </Button> 12 </content> 13 </Page> 14 </pages> 15 </App> 16 </mvc:View>
2.Controller代码
1 jQuery.sap.includeScript("/webapp/src/src/cordova.js"); 2 sap.ui.define([ 3 "zrico_appnszrico_pn_scan/controller/BaseController", 4 "sap/ui/core/mvc/Controller", 5 "sap/ui/core/UIComponent", 6 "sap/m/MessageBox", 7 "sap/ui/Device" 8 ], function(BaseController,Controller,UIComponent,MessageBox,Device) { 9 "use strict"; 10 11 return BaseController.extend("zrico_appnszrico_pn_scan.controller.GeoLocation", { 12 13 onGeoLocButton: function () { 14 //document.addEventListener("deviceready", this.onDeviceReady, false); 15 navigator.geolocation.getCurrentPosition(this.onSuccess2, this.onError2, { maximumAge: 3000, timeout: 30000}) //精度设置 enableHighAccuracy: true 16 }, 17 18 onSuccess2:function(position){ 19 MessageBox.show( 20 "Latitude: " + position.coords.latitude + " " + 21 "Longitude:" + position.coords.longitude + " " + 22 "Altitude: " + position.coords.altitude + " " + 23 "Accuracy: " + position.coords.accuracy + " " + 24 "Altitude Accuracy: " + position.coords.altitudeAccuracy + " " + 25 "Heading: " + position.coords.heading + " " + 26 "Speed: " + position.coords.speed + " " + 27 "Timestamp:" + position.timestamp + " "); 28 }, 29 30 onError2:function(error){ 31 MessageBox.show( 32 "code: " + error.code + " " + 33 "message: " + error.message + " "); 34 } 35 36 }); 37 38 });
3.SAP WEB IDE 测试
chrome谷歌浏览器执行过程中报错,应该是网络原因需要翻墙。
在IE浏览器中测试,显示正常。