React native 在ClojureScript试剂中使用React Native MapView
我正试图从使用试剂开始使用React native 在ClojureScript试剂中使用React Native MapView,react-native,clojure,android-mapview,clojurescript,reagent,React Native,Clojure,Android Mapview,Clojurescript,Reagent,我正试图从使用试剂开始使用MapView。它工作正常,但是当触发onRegionChange事件时,如何获取MapView的本地状态?试图使用当前/组件,但它总是nil (def Expo (js/require "expo")) (def map-view (r/adapt-react-class (.-MapView Expo))) (defn my-map [] (r/create-class {:component-did-mount (fn [this]
MapView
。它工作正常,但是当触发onRegionChange
事件时,如何获取MapView
的本地状态?试图使用当前/组件,但它总是nil
(def Expo (js/require "expo"))
(def map-view (r/adapt-react-class (.-MapView Expo)))
(defn my-map []
(r/create-class
{:component-did-mount (fn [this]
(println "component mount "))
:reagent-render (fn []
(let [this (r/current-component)]
[map-view {:style {:flex 1}
:initialRegion {:latitude 37.78825
:longitude -122.4324
:latitudeDelta 0.0922
:longitudeDelta 0.0421}
:onRegionChange (fn []
;; Would like to see state here.
(println (.. this -state)) )}]))}))
区域信息
onRegionChange
回调将Region
作为参数<代码>区域具有以下签名:
type Region {
latitude: Number,
longitude: Number,
latitudeDelta: Number,
longitudeDelta: Number,
}
可以使用从区域中获取值
如果您获取区域
并从中提取值,则您的我的地图
如下所示:
(defn my-map []
[map-view {:style {:flex 1}
:initialRegion {:latitude 52.3702
:longitude 4.89516
:latitudeDelta 0.0922
:longitudeDelta 0.0421}
:onRegionChange (fn [region]
(alert (str "Latitude: "
(goog.object/get region "latitude")
"\nLongitude: "
(goog.object/get region "longitude"))))}])
您可以以相同的方式获取latitudelta
和longtudelta
拖动地图时,将显示纬度和经度:
组件
如果您希望访问组件本身,您的代码可以正常工作,您只需:
这会打印出如下内容:
不确定您是否可以对该组件进行任何操作,我认为您需要
区域
信息。为什么需要expo并在其上调用-MapView
?这是expo文档以获取更多信息,谢谢。今晚我来看看。谢谢你的辛勤工作。但我的问题是得到当地的州,而不是地区。因为本地状态有子组件、区域、标记等。我需要该组件的所有本地状态。我怎样才能得到当地的州?谢谢你的提问很高兴我现在也在编写React原生应用程序的路上。啊哈,我明白了。当我查看此
时,“状态”为nil
,也就是当有可用的标记时。也许您可以创建自己的回调函数重写,该函数在:componentDidMount
中具有更多状态。这是一个开始:。
(defn jsx->clj
[x]
(into {} (for [k (.keys js/Object x)] [k (aget x k)])))
(defn my-map []
(let [this (r/current-component)]
[map-view {:style {:flex 1}
:initialRegion {:latitude 37.78825
:longitude -122.4324
:latitudeDelta 0.0922
:longitudeDelta 0.0421}
:onRegionChange (fn [region]
;; Would like to see state here.
(alert "The component..."
(str (jsx->clj this))))}]))