123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763 |
- <template>
- <view class="content">
- <map class="map" id="map1" ref="map1" :longitude="location.longitude" :latitude="location.latitude" :scale="scale"
- :markers="markers" :include-points="includePoints" :polyline="polyline" :polygons="polygons" :circles="circles"
- :controls="controls" :show-location="showLocation" :enable-3D="enable3D" :rotate="rotate" :skew="skew"
- :show-compass="showCompass" :enable-overlooking="enableOverlooking" :enable-zoom="enableZoom"
- :enable-scroll="enableScroll" :enable-rotate="enableRotate" :enable-satellite="enableSatellite"
- :enable-traffic="enableTraffic" @markertap="onmarkertap" @callouttap="oncallouttap" @controltap="oncontroltap"
- @regionchange="onregionchange" @tap="maptap" @updated="onupdated" @poitap="onpoitap"></map>
- <scroll-view class="scrollview" scroll-y="true">
- <view class="tips">注意:App和Web需要正确配置地图服务商的Key才能显示地图组件</view>
- <view class="uni-title">
- <text class="uni-title-text">属性示例</text>
- </View>
- <input-data defaultValue="13" title="scale: 缩放级别,取值范围为5-18" type="number"
- @confirm="confirm_scale_input"></input-data>
- <boolean-data :defaultValue="showLocation" title="开启显示带有方向的当前定位点" @change="change_show_location"></boolean-data>
- <boolean-data :default-value="enable3D" :disabled="enableSatellite" title="3D效果(放大缩放级别才能看到建筑物3D效果)" @change="change_enable_3d"></boolean-data>
- <boolean-data :default-value="showCompass" title="显示指南针" @change="change_show_campass"></boolean-data>
- <boolean-data :default-value="enableOverlooking" title="俯视支持" @change="change_enable_overlooking"></boolean-data>
- <boolean-data :default-value="enableRotate" title="旋转支持" @change="change_enable_rotate"></boolean-data>
- <boolean-data :default-value="enableZoom" title="缩放支持" @change="change_enable_zoom"></boolean-data>
- <boolean-data :default-value="enableScroll" title="拖动支持" @change="change_enable_scroll"></boolean-data>
- <boolean-data :default-value="enableSatellite" title="卫星图" @change="change_enable_satellite"></boolean-data>
- <boolean-data :default-value="enableTraffic" title="实时路况" @change="change_enable_traffic"></boolean-data>
- <button class="button" @click="addControls">控件</button>
- <button class="button" @click="addMarkers">添加标记点</button>
- <!-- #ifdef WEB || MP -->
- <button class="button" @click="addMarkersLabel">为标记点旁边增加标签</button>
- <!-- #endif -->
- <button class="button" @click="removeMarker">移除ID为4的标记点和标签</button>
- <button class="button" @click="addPolyline">添加路线</button>
- <button class="button" @click="removePolyline">移除一条路线</button>
- <button class="button" @click="addPolygons">添加多边形</button>
- <button class="button" @click="removePolygon">移除一个多边形</button>
- <button class="button" @click="addCircles">添加圆</button>
- <button class="button" @click="removeCircle">移除一个圆</button>
- <button class="button" @click="includePoint">缩放视野以包含所有给定的坐标点</button>
- <view class="uni-title">
- <text class="uni-title-text">方法示例</text>
- </View>
- <button class="button" @click="handleGetCenterLocation">获取当前地图中心的经纬度</button>
- <button class="button" @click="handleGetRegion">获取当前地图的视野范围</button>
- <button class="button" @click="handleTranslateMarker">平移marker</button>
- <button class="button" @click="handleMoveToLocation">将地图中心移动到指定坐标</button>
- <button class="button" @click="handleMoveToMyLocation">将地图中心移动到当前位置</button>
- <button class="button" @click="handleGetScale">获取当前地图的缩放级别</button>
- </scroll-view>
- </view>
- </template>
- <script setup lang="uts">
- type TypeJestResult = {
- translateMarkerMsg : string,
- animationEnd : boolean,
- centerPoints : LocationObject,
- southwest : LocationObject,
- northeast : LocationObject,
- moveToLocationMsg : string,
- scale : number,
- eventDetailJsonStringify: string,
- }
- const testMarkers :Marker[]= [{
- id: 0,
- latitude: 39.989631,
- longitude: 116.481018,
- title: '方恒国际 阜通东大街6号',
- iconPath: '../../../static/location.png',
- rotate: 0,
- width: 20,
- height: 20,
- anchor: {
- x: 0.5,
- y: 1
- } ,
- callout: {
- content: '方恒国际 阜通东大街6号',
- color: '#00BFFF',
- fontSize: 10,
- borderRadius: 4,
- borderWidth: 1,
- borderColor: '#333300',
- bgColor: '#CCFF99',
- padding: 5,
- display: 'ALWAYS'
- } as MapMarkerCallout
- },
- {
- id: 1,
- latitude: 39.9086920000,
- longitude: 116.3974770000,
- title: '天安门',
- // zIndex: '1',
- iconPath: '../../../static/location.png',
- width: 40,
- height: 40,
- anchor: {
- x: 0.5,
- y: 1
- },
- callout: {
- content: '首都北京\n天安门',
- color: '#00BFFF',
- fontSize: 12,
- borderRadius: 10,
- borderWidth: 2,
- borderColor: '#333300',
- bgColor: '#CCFF11',
- padding: 5,
- display: 'ALWAYS'
- } as MapMarkerCallout
- },
- {
- id: 2,
- latitude: 39.894793,
- longitude: 116.321592,
- title: '北京西站',
- iconPath: '../../../static/location.png',
- width: 40,
- height: 40,
- anchor: {
- x: 0.5,
- y: 1
- },
- callout: {
- content: '北京西站',
- color: '#ffffff',
- fontSize: 12,
- borderRadius: 10,
- borderWidth: 2,
- borderColor: '#333300',
- bgColor: '#ff5500',
- padding: 5,
- display: 'ALWAYS'
- } as MapMarkerCallout
- },
- {
- id: 3,
- latitude: 39.902344,
- longitude: 116.484822,
- title: '北京东站',
- iconPath: '../../../static/location.png',
- width: 20,
- height: 20,
- anchor: {
- x: 0.5,
- y: 1
- },
- callout: {
- content: '北京东站',
- color: '#ffffff',
- fontSize: 12,
- borderRadius: 10,
- borderWidth: 2,
- borderColor: '#333300',
- bgColor: '#ff5500',
- padding: 5,
- display: 'ALWAYS'
- } as MapMarkerCallout
- },
- {
- id: 4,
- latitude: 39.865011,
- longitude: 116.379007,
- title: '北京南站',
- iconPath: '../../../static/location.png',
- width: 40,
- height: 40,
- anchor: {
- x: 0.5,
- y: 1
- },
- callout: {
- content: '北京南站',
- color: '#ffffff',
- fontSize: 12,
- borderRadius: 10,
- borderWidth: 2,
- borderColor: '#333300',
- bgColor: '#00aa00',
- padding: 5,
- display: 'ALWAYS'
- } as MapMarkerCallout
- },
- ];
- const testPolyline: Polyline[] = [{
- points: [{
- latitude: 39.925539,
- longitude: 116.279037
- },
- {
- latitude: 39.925539,
- longitude: 116.520285
- }],
- color: '#FFCCFF',
- width: 7,
- dottedLine: true,
- arrowLine: true,
- borderColor: '#000000',
- borderWidth: 2
- },
- {
- points: [{
- latitude: 39.938698,
- longitude: 116.275177
- },
- {
- latitude: 39.966069,
- longitude: 116.289253
- },
- {
- latitude: 39.944226,
- longitude: 116.306076
- },
- {
- latitude: 39.966069,
- longitude: 116.322899
- },
- {
- latitude: 39.938698,
- longitude: 116.336975
- }],
- color: '#CCFFFF',
- width: 5,
- dottedLine: false,
- arrowLine: true,
- borderColor: '#CC0000',
- borderWidth: 3
- }
- ];
- const testPolygons : Polygon[] = [{
- points: [{
- latitude: 39.781892,
- longitude: 116.293413
- },
- {
- latitude: 39.787600,
- longitude: 116.391842
- },
- {
- latitude: 39.733187,
- longitude: 116.417932
- },
- {
- latitude: 39.704653,
- longitude: 116.338255
- }],
- fillColor: '#FFCCFF',
- strokeWidth: 3,
- strokeColor: '#CC99CC',
- zIndex: 11
- },
- {
- points: [{
- latitude: 39.887600,
- longitude: 116.518932
- },
- {
- latitude: 39.781892,
- longitude: 116.518932
- },
- {
- latitude: 39.781892,
- longitude: 116.428932
- },
- {
- latitude: 39.887600,
- longitude: 116.428932
- }
- ],
- fillColor: '#CCFFFF',
- strokeWidth: 5,
- strokeColor: '#CC0000',
- zIndex: 3
- }
- ];
- const testCircles: Circle[] = [{
- latitude: 39.996441,
- longitude: 116.411146,
- radius: 15000,
- strokeWidth: 5,
- color: '#CCFFFF',
- fillColor: '#CC0000'
- },
- {
- latitude: 40.096441,
- longitude: 116.511146,
- radius: 12000,
- strokeWidth: 3,
- color: '#CCFFFF',
- fillColor: '#FFCCFF'
- },
- {
- latitude: 39.896441,
- longitude: 116.311146,
- radius: 9000,
- strokeWidth: 1,
- color: '#CCFFFF',
- fillColor: '#CC0000'
- }
- ];
- const testIncludePoints :LocationObject[] = [{
- latitude: 39.989631,
- longitude: 116.481018,
- },
- {
- latitude: 39.9086920000,
- longitude: 116.3974770000,
- }
- ];
- const mapContext = ref(null as MapContext | null);
- const location = ref({ longitude: 116.39742, latitude: 39.909 } as LocationObject);
- const rotate = ref(0);
- const skew = ref(0);
- // 自动化测试
- const autoTest = ref(false);
- const updateAutoTest = (value : boolean) => {
- autoTest.value = value
- }
- const jestResult = reactive({
- translateMarkerMsg: "",
- animationEnd: false,
- centerPoints: {
- latitude: 0,
- longitude: 0
- },
- southwest: {
- latitude: 0,
- longitude: 0
- },
- northeast: {
- latitude: 0,
- longitude: 0
- },
- moveToLocationMsg: "",
- scale: 0,
- eventDetailJsonStringify: "{}"
- } as TypeJestResult);
- onReady(() => {
- mapContext.value = uni.createMapContext("map1", getCurrentInstance()!.proxy!)
- });
- const scale = ref(13);
- const confirm_scale_input = (value : number) => {
- scale.value = value
- };
- const controls = ref([] as Control[]);
- const addControls = () => {
- controls.value = [{
- id: 1,
- position: {
- left: 5,
- top: 180,
- width: 30,
- height: 30
- } as ControlPosition,
- iconPath: '../../../static/uni.png',
- clickable: true
- } as Control]
- }
- const showLocation = ref(false);
- const enable3D = ref(false);
- const showCompass = ref(false);
- const enableOverlooking = ref(false);
- const enableRotate = ref(false);
- const enableZoom = ref(true);
- const enableScroll = ref(true);
- const enableSatellite = ref(false);
- const enableTraffic = ref(false);
- const change_show_location = (checked : boolean) => {
- showLocation.value = checked
- }
- const change_enable_3d = (checked : boolean) => {
- enable3D.value = checked
- }
- const change_show_campass = (checked : boolean) => {
- showCompass.value = checked
- }
- const change_enable_overlooking = (checked : boolean) => {
- enableOverlooking.value = checked
- }
- const change_enable_rotate = (checked : boolean) => {
- enableRotate.value = checked
- }
- const change_enable_zoom = (checked : boolean) => {
- enableZoom.value = checked
- }
- const change_enable_scroll = (checked : boolean) => {
- enableScroll.value = checked
- }
- const change_enable_satellite = (checked : boolean) => {
- enableSatellite.value = checked
- }
- const change_enable_traffic = (checked : boolean) => {
- enableTraffic.value = checked
- }
- const includePoints = ref([] as LocationObject[]);
- const includePoint = () => {
- includePoints.value = testIncludePoints;
- };
- let markers = ref([] as Marker[]);
- const addMarkers = () => {
- scale.value = 11
- const temp = JSON.parse<Marker[]>(JSON.stringify(testMarkers))!
- markers.value = temp
- };
- const removeMarker = () => {
- // #ifdef APP
- mapContext.value?.removeMarkers({markerIds:[4]})
- // #endif
- // #ifdef WEB || MP
- const index = markers.value.findIndex((marker: Marker):boolean=>{
- return marker.id == 4
- });
- if (index != -1) {
- delete markers.value[index].label;
- markers.value.splice(index, 1);
- } else {
- uni.showToast({
- title: '未找到该标记点',
- icon: 'none'
- });
- }
- // #endif
- };
- // #ifdef WEB || MP
- const addMarkersLabel = () => {
- markers.value.forEach((marker, index) => {
- marker.label = {
- content: 'Hello Label' + (index + 1),
- color: '#aa00ff',
- fontSize: 12,
- x: 5,
- y: 0,
- borderColor: '#333300',
- borderWidth: 2,
- borderRadius: 20,
- bgColor: '#aaffff',
- padding: 10
- };
- });
- };
- // #endif
- const polyline = ref([] as Polyline[]);
- const addPolyline = () => {
- scale.value = 11;
- polyline.value = JSON.parse<Polyline[]>(JSON.stringify(testPolyline))!;
- };
- const removePolyline = () => {
- if (polyline.value.length > 1) {
- polyline.value = JSON.parse<Polyline[]>(JSON.stringify(testPolyline))!.splice(0, 1);
- }
- };
- const polygons = ref([] as Polygon[]);
- const addPolygons = () => {
- scale.value = 10;
- polygons.value = JSON.parse<Polygon[]>(JSON.stringify(testPolygons))!;
- };
- const removePolygon = () => {
- if (polygons.value.length > 1) {
- polygons.value = JSON.parse<Polygon[]>(JSON.stringify(testPolygons))!.splice(0, 1);
- }
- };
- const circles = ref([] as Circle[]);
- const addCircles = () => {
- scale.value = 10;
- circles.value = JSON.parse<Circle[]>(JSON.stringify(testCircles))!;
- };
- const removeCircle = () => {
- if (circles.value.length > 1) {
- circles.value = JSON.parse<Circle[]>(JSON.stringify(testCircles))!.slice(1);
- }
- };
- const handleGetCenterLocation = () => {
- mapContext.value?.getCenterLocation({
- success: ret => {
- // console.log('getCenterLocation',ret);
- jestResult.centerPoints = ret;
- if (!autoTest.value) {
- uni.showModal({
- content: JSON.stringify(ret)
- });
- }
- }
- });
- };
- const handleGetRegion = () => {
- mapContext.value?.getRegion({
- success: ret => {
- // console.log('getRegion',JSON.stringify(ret));
- jestResult.southwest = ret.southwest;
- jestResult.northeast = ret.northeast
- if (!autoTest.value) {
- uni.showModal({
- content: JSON.stringify(ret)
- });
- }
- }
- });
- };
- const handleTranslateMarker = () => {
- mapContext.value?.translateMarker({
- markerId: 1,
- destination: {
- latitude: 39.989631,
- longitude: 116.481018
- },
- autoRotate: true,
- rotate: 10,
- duration: 2000,
- moveWithRotate: true,
- // #ifdef WEB || MP
- animationEnd: () => {
- console.log('动画结束');
- jestResult.animationEnd = true;
- },
- // #endif
- success: ret => {
- console.log('handleTranslateMarker',JSON.stringify(ret));
- const result = ret as UTSJSONObject
- jestResult.translateMarkerMsg = result["errMsg"] as string;
- },
- fail: error => {
- console.log(error)
- }
- });
- mapContext.value?.translateMarker({
- markerId: 2,
- destination: {
- latitude: 39.902344,
- longitude: 116.484822,
- },
- autoRotate: true,
- rotate: 0,
- duration: 2000,
- success: ret => {
- console.log('handleTranslateMarker', JSON.stringify(ret));
- },
- fail: error => {
- console.log(error)
- }
- });
- };
- const handleGetScale = () => {
- mapContext.value?.getScale({
- success: res => {
- // console.log('getScale',res);
- scale.value = res.scale
- jestResult.scale = res.scale
- if (!autoTest.value) {
- uni.showModal({
- content: '当前地图的缩放级别为:' + res.scale
- });
- }
- },
- fail: error => {
- console.log(error)
- },
- });
- };
- const handleMoveToLocation = () => {
- mapContext.value?.moveToLocation({
- latitude: 39.909,
- longitude: 116.39742,
- success: res => {
- console.log('moveToLocation',res);
- const result = res as UTSJSONObject
- jestResult.moveToLocationMsg = result["errMsg"] as string;
- if (!autoTest.value) {
- uni.showModal({
- content: JSON.stringify(res)
- });
- }
- },
- fail: error => {
- console.log(error)
- }
- });
- };
- const handleMoveToMyLocation = () => {
- mapContext.value?.moveToLocation({
- success: res => {
- uni.showModal({
- content: '地图中心已经移动到当前位置'
- })
- },
- fail: error => {
- console.log(error);
- }
- })
- }
- const maptap = (e : UniMapTapEvent) => {
- // #ifdef WEB || MP
- uni.showModal({
- content: 'web端map组件tap事件不支持返回经纬度'
- });
- // #endif
- // #ifndef WEB || MP
- // console.log('点击地图时触发',e)
- //js运行环境删除多余的信息
- // #ifndef APP-ANDROID
- Reflect.deleteProperty(e, "target")
- Reflect.deleteProperty(e, "currentTarget")
- Reflect.deleteProperty(e, "_bubbles")
- Reflect.deleteProperty(e, "_cancelable")
- Reflect.deleteProperty(e, "timeStamp")
- // #endif
- uni.showModal({
- content: JSON.stringify(e)
- });
- // #endif
- };
- const onmarkertap = (e : UniMapMarkerTapEvent) => {
- // console.log('点击标记点时触发',e)
- //js运行环境删除多余的信息
- // #ifndef APP-ANDROID
- Reflect.deleteProperty(e, "target")
- Reflect.deleteProperty(e, "currentTarget")
- Reflect.deleteProperty(e, "_bubbles")
- Reflect.deleteProperty(e, "_cancelable")
- Reflect.deleteProperty(e, "timeStamp")
- // #endif
- uni.showModal({
- content: JSON.stringify(e)
- });
- };
- const oncontroltap = (e : UniMapControlTapEvent) => {
- // console.log('点击控件时触发',e)
- uni.showModal({
- content: JSON.stringify(e.detail)
- });
- };
- const oncallouttap = (e : UniMapCalloutTapEvent) => {
- // console.log('点击标记点对应的气泡时触发',e)
- uni.showModal({
- content: JSON.stringify(e.detail)
- });
- };
- const onupdated = (e : UniMapUpdatedEvent) => {
- console.log('在地图渲染更新完成时触发', e)
- };
- const onregionchange = (e : UniMapRegionChangeEvent) => {
- console.log('视野发生变化时触发', e.detail)
- jestResult.eventDetailJsonStringify = JSON.stringify(e.detail)
- };
- const onpoitap = (e : UniMapPoiTapEvent) => {
- // console.log('点击地图poi点时触发',e)
- //js运行环境删除多余的信息
- // #ifndef APP-ANDROID
- Reflect.deleteProperty(e, "target")
- Reflect.deleteProperty(e, "currentTarget")
- Reflect.deleteProperty(e, "_bubbles")
- Reflect.deleteProperty(e, "_cancelable")
- Reflect.deleteProperty(e, "timeStamp")
- // #endif
- uni.showModal({
- content: JSON.stringify(e)
- });
- };
- defineExpose({
- jestResult,
- autoTest,
- updateAutoTest,
- addControls,
- addMarkers,
- // #ifdef WEB || MP
- addMarkersLabel,
- // #endif
- removeMarker,
- addPolyline,
- removePolyline,
- addPolygons,
- removePolygon,
- addCircles,
- removeCircle,
- includePoint,
- handleGetCenterLocation,
- handleGetRegion,
- handleTranslateMarker,
- handleMoveToLocation,
- handleGetScale
- })
- </script>
- <style>
- .content {
- flex: 1;
- }
- .map {
- width: 100%;
- height: 300px;
- background-color: #f0f0f0;
- }
- .scrollview {
- flex: 1;
- padding: 10px;
- }
- .button {
- margin-top: 5px;
- margin-bottom: 5px;
- }
- .tips {
- font-size: 12px;
- margin-top: 15px;
- opacity: .8;
- }
- </style>
|