import { Laya } from "Laya" ; import { Geolocation } from "laya/device/geolocation/Geolocation" ; import { GeolocationInfo } from "laya/device/geolocation/GeolocationInfo" ; import { Stage } from "laya/display/Stage" ; import { Text } from "laya/display/Text" ; import { Browser } from "laya/utils/Browser" ; import { Handler } from "laya/utils/Handler" ; import { Main } from "./../Main" ; author Survivor
  export class InputDevice_Map { private map : any ; private marker : any ; private BMap : any = Browser . window . BMap ; private convertor : any = new this . BMap . Convertor ( ) ; private mapDiv : any ; private infoText : Text ; Main : typeof Main = null ; constructor ( maincls ) { this . Main = maincls ; Laya . init ( Browser . width , 255 ) ; Laya . stage . scaleMode = Stage . SCALE_NOSCALE ; this . createDom ( ) ; this . initMap ( ) ; this . createInfoText ( ) ; var successHandler = new Handler ( this , this . updatePosition ) ; var errorHandler = new Handler ( this , this . onError ) ; Geolocation . enableHighAccuracy = true ; Geolocation . watchPosition ( successHandler , errorHandler ) ; this . convertToBaiduCoord = this . convertToBaiduCoord . bind ( this ) ; } private createDom ( ) { this . mapDiv = Browser . createElement ( "div" ) ; var style = this . mapDiv . style ; style . position = "absolute" ; style . top = Laya . stage . height / Browser . pixelRatio + "px" ; style . left = "0px" ; style . width = Browser . width / Browser . pixelRatio + "px" ; style . height = ( Browser . height - Laya . stage . height ) / Browser . pixelRatio + "px" ; Browser . document . body . appendChild ( this . mapDiv ) ; } private initMap ( ) { this . map = new this . BMap . Map ( this . mapDiv ) ; this . map . disableKeyboard ( ) ; this . map . disableScrollWheelZoom ( ) ; this . map . disableDoubleClickZoom ( ) ; this . map . disablePinchToZoom ( ) ; this . map . centerAndZoom ( new this . BMap . Point ( 116.32715863448607 , 39.990912172420714 ) , 15 ) ; this . marker = new this . BMap . Marker ( new this . BMap . Point ( 0 , 0 ) ) ; this . map . addOverlay ( this . marker ) ; var label = new this . BMap . Label ( "当前位置" , { "offset" : new this . BMap . Size ( - 15 , 30 ) } ) ; this . marker . setLabel ( label ) ; } private createInfoText ( ) { this . infoText = new Text ( ) ; this . Main . box2D . addChild ( this . infoText ) ; this . infoText . fontSize = 50 ; this . infoText . color = "#FFFFFF" ; this . infoText . size ( Laya . stage . width , Laya . stage . height ) ; } private updatePosition ( p ) { var point = new this . BMap . Point ( p . longitude , p . latitude ) ; this . convertor . translate ( [ point ] , 1 , 5 , this . convertToBaiduCoord ) ; this . infoText . text = "经度：" + p . longitude + "\t纬度：" + p . latitude + "\t精度：" + p . accuracy + "\n海拔：" + p . altitude + "\t海拔精度：" + p . altitudeAccuracy + "\n头：" + p . heading + "\n速度：" + p . speed + "\n时间戳：" + p . timestamp ; } private convertToBaiduCoord ( data ) { if ( data . status == 0 ) { var position = data . points [ 0 ] ; this . marker . setPosition ( position ) ; this . map . panTo ( position ) ; this . map . setZoom ( 17 ) ; } } private onError ( e ) { if ( e . code == Geolocation . TIMEOUT ) alert ( "获取位置超时" ) ; else if ( e . code == Geolocation . POSITION_UNAVAILABLE ) alert ( "位置不可用" ) ; else if ( e . code == Geolocation . PERMISSION_DENIED ) alert ( "无权限" ) ; } }