亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

設置嘗試后無法設置對象狀態 useState 反應本機地圖

設置嘗試后無法設置對象狀態 useState 反應本機地圖

翻過高山走不出你 2021-12-12 16:06:09
我似乎無法使用 useState 鉤子設置值,正如您在 pickLocation 輸出中看到的那樣。當我控制 pickLocation 時,我得到了初始值。即使設置了初始值,我的地圖也沒有顯示。在 IOS 上,根據我的 isFetching 狀態,我的消息是“尚未選擇地圖”,而 android 則存在不同的地圖錯誤Error using newLatLngBounds(...我在這里嘗試了答案:https : //stackoverflow.com/a/54069332/2277245 使用 useEffect 和 spread 運算符,但它們也不起作用。有人幫忙嗎?謝謝
查看完整描述

1 回答

?
森欄

TA貢獻1810條經驗 獲得超5個贊

我通過復制和改編來自官方世博會文檔https://docs.expo.io/versions/v35.0.0/sdk/location/的示例解決了這個問題


import React, { useState, useEffect } from "react";

import { StyleSheet, Platform } from "react-native";

import { useSelector } from "react-redux";

import MapView, { Marker } from "react-native-maps";

import Constants from "expo-constants";

import * as Location from "expo-location";

import * as Permissions from "expo-permissions";


const MapScreen = props => {

  const availableVenues = useSelector(state => state.cafes.availableVenues);


  const [isLocation, setLocation] = useState(null);


  const [mapRegion, setMapRegion] = useState({

    region: {

      latitude: -37.813629,

      longitude: 144.963058,

      latitudeDelta: 0.0922,

      longitudeDelta: 0.0421

    }

  });


  const selectItemHandler = (id, title) => {

    props.navigation.navigate("VenueDetail", {

      venueId: id,

      venueTitle: title

    });

  };


  useEffect(() => {

    if (Platform.OS === "android" && !Constants.isDevice) {

      Alert.alert(

        "Error",

        "Oops, this will not work on Sketch in an Android emulator. Try it on your device!",

        [

          {

            text: "Okay"

          }

        ]

      );

    } else {

      this._getLocationAsync();

    }

  }, []);


  _getLocationAsync = async () => {

    let { status } = await Permissions.askAsync(Permissions.LOCATION);

    if (status !== "granted") {

      Alert.alert(

        "Insufficient Permissions",

        "Sorry, we need location permissions to make this work!",

        [

          {

            text: "Okay"

          }

        ]

      );

    }

    let location = await Location.getCurrentPositionAsync({});

    setLocation({ location });

    setMapRegion({

      region: {

        latitude: location.coords.latitude,

        longitude: location.coords.longitude,

        latitudeDelta: 0.0922,

        longitudeDelta: 0.0421

      }

    });

    console.log(mapRegion);

  };


  return (

    <MapView

      style={styles.map}

      showsUserLocation={true}

      initialRegion={mapRegion.region}

      region={mapRegion.region}

    >

      {availableVenues.map(marker => (

        <Marker

          key={marker.id}

          coordinate={{ latitude: marker.lat, longitude: marker.lng }}

          title={marker.title}

          description={marker.address}

          onPress={() => { //Need to change to press popup

            setTimeout(() => {

              selectItemHandler(marker.id, marker.title);

            }, 5000);

          }}

        />

      ))}

    </MapView>

  );

};


MapScreen.navigationOptions = {

  headerTitle: "Venue Map"

};


const styles = StyleSheet.create({

  map: {

    flex: 1,

    justifyContent: "center",

    alignItems: "center"

  },

  container: {

    flex: 1,

    alignItems: "center",

    justifyContent: "center",

    paddingTop: Constants.statusBarHeight,

    backgroundColor: "#ecf0f1"

  },

  paragraph: {

    margin: 24,

    fontSize: 18,

    textAlign: "center",

    color: "black"

  }

});


export default MapScreen;



查看完整回答
反對 回復 2021-12-12
  • 1 回答
  • 0 關注
  • 306 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號