Skip to content

Commit

Permalink
fix(h5): 避免地图选择器重复注入
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakaryCode committed May 29, 2023
1 parent 35da2d3 commit ce1113b
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/taro-h5/src/api/location/chooseLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { stringify } from 'query-string'

import { MethodHandler } from '../../utils/handler'

let container: HTMLDivElement | null = null
function createLocationChooser (handler, key = LOCATION_APIKEY, mapOpt: Taro.chooseLocation.Option['mapOpts'] = {}) {
const { latitude, longitude, ...opts } = mapOpt
const query = {
Expand All @@ -14,7 +15,8 @@ function createLocationChooser (handler, key = LOCATION_APIKEY, mapOpt: Taro.cho
referer: 'myapp',
...opts
}
const html = `
if (!container) {
const html = `
<div class='taro_choose_location'>
<div class='taro_choose_location_bar'>
<div class='taro_choose_location_back'></div>
Expand All @@ -24,8 +26,9 @@ function createLocationChooser (handler, key = LOCATION_APIKEY, mapOpt: Taro.cho
<iframe class='taro_choose_location_frame' frameborder='0' src="https://apis.map.qq.com/tools/locpicker?${stringify(query, { arrayFormat: 'comma', skipNull: true })}" />
</div>
`
const container = document.createElement('div')
container.innerHTML = html
container = document.createElement('div')
container.innerHTML = html
}
const main: HTMLDivElement = container.querySelector('.taro_choose_location') as HTMLDivElement

function show () {
Expand All @@ -49,7 +52,8 @@ function createLocationChooser (handler, key = LOCATION_APIKEY, mapOpt: Taro.cho
}

function remove () {
container.remove()
container?.remove()
container = null
window.removeEventListener('popstate', back)
}

Expand All @@ -61,7 +65,7 @@ function createLocationChooser (handler, key = LOCATION_APIKEY, mapOpt: Taro.cho
return {
show,
remove,
container
container,
}
}

Expand Down

0 comments on commit ce1113b

Please sign in to comment.