当我按住以拖动以响应本机地图时,Google地图标记移动错误

时间:2019-12-18 21:47:22

标签: reactjs react-native google-maps android-emulator react-native-maps

我想在react native mapview中拖动一个标记。
拖动是可以的,但是当我第一次按下标记时,它会稍微向上方移动。
我想解决这个意外的动作。
我不确定是什么原因。 您可以查看当前情况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

1 个答案:

答案 0 :(得分:1)

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

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

希望这会有所帮助!