Skip to content

定位模块

数据结构

ESLocationInfo

位置信息

参数描述类型非空
address地址string
province省份string
city城市string
district地区string
name名称string
longitude经度number
latitude纬度number

接口

getLocation

该方法获取位置信息。

完整方法声明:function getLocation(): Promise<ESLocationInfo | null>

  • 参数:

  • 返回值:
属性描述类型默认值
位置信息Promise<ESLocationInfo | null>

launchLocation

该方法打开定位设置。

完整方法声明:function launchLocation(): void

  • 参数:

  • 返回值:
属性描述类型默认值
void

addListener

该方法添加定位监听。

完整方法声明:function addListener(listener: ESLocationListener): void

  • 参数:
参数描述类型非空
listener定位监听ESLocationListener
  • 返回值:
属性描述类型默认值
void

removeListener

该方法删除定位监听。

完整方法声明:function removeListener(listener: ESLocationListener): void

  • 参数:
参数描述类型非空
listener定位监听ESLocationListener
  • 返回值:
属性描述类型默认值
void

基础用法

  • 代码示例:
    点击查看源码
    vue
    <template>
    	<div class='es-sdk-root-css'>
    		<s-title-view class='es-sdk-content-title-css' :text='this.$options.name' />
    		<div class='es-sdk-content-divider-css' />
    		<div class='es-sdk-content-column-css'>
    			<s-text-view :text="'当前位置:' + currentLocation"></s-text-view>
    			<div class='es-sdk-content-row-css'>
    				<s-text-button text='打开定位设置' @onButtonClicked='onLaunchLocation' />
    				<s-text-button text='获取定位信息' @onButtonClicked='onGetLocation' />
    			</div>
    		</div>
    	</div>
    </template>
    
    <script lang='ts'>
    
    import {defineComponent} from '@vue/runtime-core';
    import {ESLocationInfo, ESLocationListener, useESLocation} from '@extscreen/es3-core';
    import {ref} from 'vue';
    
    export default defineComponent({
    	name: '定位模块',
    	setup() {
    
    		const location = useESLocation();
    		const currentLocation = ref('');
    
    		function onLaunchLocation() {
    			location.launchLocation();
    		}
    
    		function onGetLocation() {
    			location.getLocation().then((location) => {
    				currentLocation.value = JSON.stringify(location);
    			}, error => {
    				currentLocation.value = error + '';
    			});
    		}
    
    		//监听地址变化
    		const listener: ESLocationListener = {
    			onLocationChange(location: ESLocationInfo | null) {
    				currentLocation.value = JSON.stringify(location);
    			},
    		};
    
    		const onESCreate = (params) => {
    			location.addListener(listener);
    		};
    
    		const onESDestroy = () => {
    			location.removeListener(listener);
    		};
    
    		return {
    			onESCreate,
    			onESDestroy,
    			onLaunchLocation,
    			onGetLocation,
    			currentLocation,
    		};
    	},
    });
    
    </script>
    <style>
    </style>