3

I am trying to use MapView from https://github.com/airbnb/react-native-maps using Reagent. It works fine but how could I get local state for MapView when onRegionChange event is fired? Trying to use current/component but it is always 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))                                                       )}]))}))
3
  • Why are you requiring expo and calling .-MapView on it? Commented Sep 20, 2017 at 21:50
  • 1
    Here is expo documentation for more info docs.expo.io/versions/latest/sdk/map-view.html Commented Sep 22, 2017 at 3:18
  • Ah thanks. I'll look at it this evening. Commented Sep 22, 2017 at 9:44

1 Answer 1

1

Region information

The onRegionChange callback has Region as argument. Region has the following signature:

type Region {
  latitude: Number,
  longitude: Number,
  latitudeDelta: Number,
  longitudeDelta: Number,
}

You can get the values from the Region by using goog.object/get.

If you get the region and extract the values from it your my-map looks like:

(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"))))}])

You can obtain the latitudeDelta and longitudeDelta in the same manner.

When you drag the map the latitude and longitude show up:

react-native-maps

The component

If you want access to the component itself your code works fine, you just have to visualize it:

(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))))}]))

This prints something like:

Print component itself

Not sure if you can do anything with the component, I think you need the Region information.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your hard work. But my problem was to get local state not region. Because local state has child component, region, markers etc. I need all local state of that component. How do I get local state?
Thank you for the question. ;) Glad I am now on my way to programming a React Native app as well. And aha I see. When I look in this the "state" is nil, also when there are markers available. Maybe you can create your own override of a callback function that has more state in :componentDidMount. This is a begin: stackoverflow.com/questions/43176862/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.