[我坚持按下以拖动以反应本机地图时,Google地图标记移动错误

问题描述 投票:1回答:1

我想在反应本地地图视图中拖动标记。拖动是可以的,但是当我第一次按下标记时,它会稍微向上方移动。我想解决这个意外的举动。我不确定是什么原因。您可以检查当前情况here。源代码如下。

import React, {useState, useEffect} from 'react'
import { View } from 'react-native'
import MapView, {Marker} from 'react-native-maps'

const DEFAULT_DELTA = {latitudeDelta: 0.015, longitudeDelta: 0.0121}

const initialLoc = {
  latitude: 24,
  longitude: 75
}

const App = () => {
  const [location, setLocation] = useState(initialLoc)

  useEffect(() => {    
    navigator.geolocation.watchPosition(
      position => {
        const {latitude, longitude} = position.coords;
        setLocation({
          latitude,
          longitude
        })          
      },
      error => { 
        console.error(error)
      }, {
        enableHighAccuracy: true,
        timeout: 20000,
        maximumAge: 10000
      }
    );
  }, [])

  const onMarkPress = (e) => {    
    const {coordinate} = e.nativeEvent
    setLocation(coordinate)    
  };

  const onMapPress = (e) => {    
    const {coordinate} = e.nativeEvent
    setLocation(coordinate)    
  };

  return (
    <View style={{flex:1}}>
      {location && (
        <MapView
          style={{flex: 1}}
          initialRegion={{
            latitude: location.latitude,
            longitude: location.longitude,
            ...DEFAULT_DELTA       
          }}
          onPress={onMapPress}
          >
          <Marker draggable
            coordinate={location}
            onDragEnd={onMarkPress}
          />        
        </MapView>
      )}
    </View>
  )
}

export default App
reactjs react-native google-maps android-emulator react-native-maps
1个回答
1
投票

此行为也显示在react-native-maps的DraggableMarkers.js example中,因此,此处的代码实现不是问题。

[它可能已经在GitHub存储库上报告过,但是我找不到它,所以我建议您为此here提交一个bug。

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.