creating a data and pushing in to array and showing them in a map is not working correctly?











up vote
0
down vote

favorite












Problem:



I am creating react native app with Google map integration. This is how I have done It.



import React, { Component } from "react";
import { View, Text, StyleSheet, Dimensions } from "react-native";

import { MapView } from "expo";
import Marker from "./Marker";
class Parking extends Component {
static navigationOptions = {
title: "Parking",
headerStyle: {
backgroundColor: "#06153b"
},
headerTintColor: "#fff",
headerTitleStyle: {
color: "#ffff"
}
};
constructor(props) {
super(props);
this.state = {
focusedLocation: {
latitude: 0,
longitude: 0,
latitudeDelta: 0.0122,
longitudeDelta:
(Dimensions.get("window").width / Dimensions.get("window").height) *
0.0122
},
locationChosen: false,
placesList:
};
}

componentDidMount() {
navigator.geolocation.getCurrentPosition(
pos => {
const coordsEvent = {
nativeEvent: {
coordinate: {
latitude: pos.coords.latitude,
longitude: pos.coords.longitude
}
}
};

this.pickLocationHandler(coordsEvent);
},
err => {
console.log(err);
alert("Fetching the Position failed");
}
);
}

pickLocationHandler = event => {
const coords = event.nativeEvent.coordinate;
let placesList = ;
let places = ;
this.map.animateToRegion({
...this.state.focusedLocation,
latitude: coords.latitude,
longitude: coords.longitude
});
const apikey = "My API key";
fetch(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" +
coords.latitude +
"," +
coords.longitude +
"&radius=500" +
"&type=parking" +
"&key=" +
apikey
)
.then(response => response.json())
.then(responseJson => {
if (responseJson) {
placesList = responseJson.results;
console.log(placesList);
placesList.map((el, index) => {
const place = {
title: el.name,
coordinates: {
latitude: el.geometry.location.lat,
longitude: el.geometry.location.lng
}
};
places[index] = place;
});
}
});

if (places) {
this.setState({ placesList: places });
console.log(places);
}
this.setState({ locationChosen: true });
};

render() {
let marker = null;
if (this.state.locationChosen) {
marker = <MapView.Marker coordinate={this.state.focusedLocation} />;
}
const places = this.state.placesList;
return (
<View style={styles.container}>
<MapView
initialRegion={this.state.focusedLocation}
showsUserLocation={true}
style={styles.map}
onPress={this.pickLocationHandler}
ref={ref => (this.map = ref)}
>
{places.map((place, index) => {
<MapView.Marker
coordinate={place.coordinates}
title={place.title}
/>;
})}
{marker}
</MapView>
</View>
);
}
}

export default Parking;

const styles = StyleSheet.create({
container: {
width: "100%",
alignItems: "center",
paddingBottom: 10,
paddingLeft: 10,
paddingRight: 10,
paddingTop: 10
// backgroundColor:"#192f6a"
},
map: {
height: "100%",
width: "100%"
},
button: {
margin: 8
}
});


But It is showing Nothing On the Map. When I console log the places Like this.



if (places) {
this.setState({ placesList: places });
console.log(places);
}


It shows an Empty array. If I console log the placesList inside the fetch it shows the results. Can Someone help me to solve this problem and To modify My code in order to show the markers for the places that I have got from the fetch result from google API in the map?. Thank You very Much!!.










share|improve this question


























    up vote
    0
    down vote

    favorite












    Problem:



    I am creating react native app with Google map integration. This is how I have done It.



    import React, { Component } from "react";
    import { View, Text, StyleSheet, Dimensions } from "react-native";

    import { MapView } from "expo";
    import Marker from "./Marker";
    class Parking extends Component {
    static navigationOptions = {
    title: "Parking",
    headerStyle: {
    backgroundColor: "#06153b"
    },
    headerTintColor: "#fff",
    headerTitleStyle: {
    color: "#ffff"
    }
    };
    constructor(props) {
    super(props);
    this.state = {
    focusedLocation: {
    latitude: 0,
    longitude: 0,
    latitudeDelta: 0.0122,
    longitudeDelta:
    (Dimensions.get("window").width / Dimensions.get("window").height) *
    0.0122
    },
    locationChosen: false,
    placesList:
    };
    }

    componentDidMount() {
    navigator.geolocation.getCurrentPosition(
    pos => {
    const coordsEvent = {
    nativeEvent: {
    coordinate: {
    latitude: pos.coords.latitude,
    longitude: pos.coords.longitude
    }
    }
    };

    this.pickLocationHandler(coordsEvent);
    },
    err => {
    console.log(err);
    alert("Fetching the Position failed");
    }
    );
    }

    pickLocationHandler = event => {
    const coords = event.nativeEvent.coordinate;
    let placesList = ;
    let places = ;
    this.map.animateToRegion({
    ...this.state.focusedLocation,
    latitude: coords.latitude,
    longitude: coords.longitude
    });
    const apikey = "My API key";
    fetch(
    "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" +
    coords.latitude +
    "," +
    coords.longitude +
    "&radius=500" +
    "&type=parking" +
    "&key=" +
    apikey
    )
    .then(response => response.json())
    .then(responseJson => {
    if (responseJson) {
    placesList = responseJson.results;
    console.log(placesList);
    placesList.map((el, index) => {
    const place = {
    title: el.name,
    coordinates: {
    latitude: el.geometry.location.lat,
    longitude: el.geometry.location.lng
    }
    };
    places[index] = place;
    });
    }
    });

    if (places) {
    this.setState({ placesList: places });
    console.log(places);
    }
    this.setState({ locationChosen: true });
    };

    render() {
    let marker = null;
    if (this.state.locationChosen) {
    marker = <MapView.Marker coordinate={this.state.focusedLocation} />;
    }
    const places = this.state.placesList;
    return (
    <View style={styles.container}>
    <MapView
    initialRegion={this.state.focusedLocation}
    showsUserLocation={true}
    style={styles.map}
    onPress={this.pickLocationHandler}
    ref={ref => (this.map = ref)}
    >
    {places.map((place, index) => {
    <MapView.Marker
    coordinate={place.coordinates}
    title={place.title}
    />;
    })}
    {marker}
    </MapView>
    </View>
    );
    }
    }

    export default Parking;

    const styles = StyleSheet.create({
    container: {
    width: "100%",
    alignItems: "center",
    paddingBottom: 10,
    paddingLeft: 10,
    paddingRight: 10,
    paddingTop: 10
    // backgroundColor:"#192f6a"
    },
    map: {
    height: "100%",
    width: "100%"
    },
    button: {
    margin: 8
    }
    });


    But It is showing Nothing On the Map. When I console log the places Like this.



    if (places) {
    this.setState({ placesList: places });
    console.log(places);
    }


    It shows an Empty array. If I console log the placesList inside the fetch it shows the results. Can Someone help me to solve this problem and To modify My code in order to show the markers for the places that I have got from the fetch result from google API in the map?. Thank You very Much!!.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Problem:



      I am creating react native app with Google map integration. This is how I have done It.



      import React, { Component } from "react";
      import { View, Text, StyleSheet, Dimensions } from "react-native";

      import { MapView } from "expo";
      import Marker from "./Marker";
      class Parking extends Component {
      static navigationOptions = {
      title: "Parking",
      headerStyle: {
      backgroundColor: "#06153b"
      },
      headerTintColor: "#fff",
      headerTitleStyle: {
      color: "#ffff"
      }
      };
      constructor(props) {
      super(props);
      this.state = {
      focusedLocation: {
      latitude: 0,
      longitude: 0,
      latitudeDelta: 0.0122,
      longitudeDelta:
      (Dimensions.get("window").width / Dimensions.get("window").height) *
      0.0122
      },
      locationChosen: false,
      placesList:
      };
      }

      componentDidMount() {
      navigator.geolocation.getCurrentPosition(
      pos => {
      const coordsEvent = {
      nativeEvent: {
      coordinate: {
      latitude: pos.coords.latitude,
      longitude: pos.coords.longitude
      }
      }
      };

      this.pickLocationHandler(coordsEvent);
      },
      err => {
      console.log(err);
      alert("Fetching the Position failed");
      }
      );
      }

      pickLocationHandler = event => {
      const coords = event.nativeEvent.coordinate;
      let placesList = ;
      let places = ;
      this.map.animateToRegion({
      ...this.state.focusedLocation,
      latitude: coords.latitude,
      longitude: coords.longitude
      });
      const apikey = "My API key";
      fetch(
      "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" +
      coords.latitude +
      "," +
      coords.longitude +
      "&radius=500" +
      "&type=parking" +
      "&key=" +
      apikey
      )
      .then(response => response.json())
      .then(responseJson => {
      if (responseJson) {
      placesList = responseJson.results;
      console.log(placesList);
      placesList.map((el, index) => {
      const place = {
      title: el.name,
      coordinates: {
      latitude: el.geometry.location.lat,
      longitude: el.geometry.location.lng
      }
      };
      places[index] = place;
      });
      }
      });

      if (places) {
      this.setState({ placesList: places });
      console.log(places);
      }
      this.setState({ locationChosen: true });
      };

      render() {
      let marker = null;
      if (this.state.locationChosen) {
      marker = <MapView.Marker coordinate={this.state.focusedLocation} />;
      }
      const places = this.state.placesList;
      return (
      <View style={styles.container}>
      <MapView
      initialRegion={this.state.focusedLocation}
      showsUserLocation={true}
      style={styles.map}
      onPress={this.pickLocationHandler}
      ref={ref => (this.map = ref)}
      >
      {places.map((place, index) => {
      <MapView.Marker
      coordinate={place.coordinates}
      title={place.title}
      />;
      })}
      {marker}
      </MapView>
      </View>
      );
      }
      }

      export default Parking;

      const styles = StyleSheet.create({
      container: {
      width: "100%",
      alignItems: "center",
      paddingBottom: 10,
      paddingLeft: 10,
      paddingRight: 10,
      paddingTop: 10
      // backgroundColor:"#192f6a"
      },
      map: {
      height: "100%",
      width: "100%"
      },
      button: {
      margin: 8
      }
      });


      But It is showing Nothing On the Map. When I console log the places Like this.



      if (places) {
      this.setState({ placesList: places });
      console.log(places);
      }


      It shows an Empty array. If I console log the placesList inside the fetch it shows the results. Can Someone help me to solve this problem and To modify My code in order to show the markers for the places that I have got from the fetch result from google API in the map?. Thank You very Much!!.










      share|improve this question













      Problem:



      I am creating react native app with Google map integration. This is how I have done It.



      import React, { Component } from "react";
      import { View, Text, StyleSheet, Dimensions } from "react-native";

      import { MapView } from "expo";
      import Marker from "./Marker";
      class Parking extends Component {
      static navigationOptions = {
      title: "Parking",
      headerStyle: {
      backgroundColor: "#06153b"
      },
      headerTintColor: "#fff",
      headerTitleStyle: {
      color: "#ffff"
      }
      };
      constructor(props) {
      super(props);
      this.state = {
      focusedLocation: {
      latitude: 0,
      longitude: 0,
      latitudeDelta: 0.0122,
      longitudeDelta:
      (Dimensions.get("window").width / Dimensions.get("window").height) *
      0.0122
      },
      locationChosen: false,
      placesList:
      };
      }

      componentDidMount() {
      navigator.geolocation.getCurrentPosition(
      pos => {
      const coordsEvent = {
      nativeEvent: {
      coordinate: {
      latitude: pos.coords.latitude,
      longitude: pos.coords.longitude
      }
      }
      };

      this.pickLocationHandler(coordsEvent);
      },
      err => {
      console.log(err);
      alert("Fetching the Position failed");
      }
      );
      }

      pickLocationHandler = event => {
      const coords = event.nativeEvent.coordinate;
      let placesList = ;
      let places = ;
      this.map.animateToRegion({
      ...this.state.focusedLocation,
      latitude: coords.latitude,
      longitude: coords.longitude
      });
      const apikey = "My API key";
      fetch(
      "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" +
      coords.latitude +
      "," +
      coords.longitude +
      "&radius=500" +
      "&type=parking" +
      "&key=" +
      apikey
      )
      .then(response => response.json())
      .then(responseJson => {
      if (responseJson) {
      placesList = responseJson.results;
      console.log(placesList);
      placesList.map((el, index) => {
      const place = {
      title: el.name,
      coordinates: {
      latitude: el.geometry.location.lat,
      longitude: el.geometry.location.lng
      }
      };
      places[index] = place;
      });
      }
      });

      if (places) {
      this.setState({ placesList: places });
      console.log(places);
      }
      this.setState({ locationChosen: true });
      };

      render() {
      let marker = null;
      if (this.state.locationChosen) {
      marker = <MapView.Marker coordinate={this.state.focusedLocation} />;
      }
      const places = this.state.placesList;
      return (
      <View style={styles.container}>
      <MapView
      initialRegion={this.state.focusedLocation}
      showsUserLocation={true}
      style={styles.map}
      onPress={this.pickLocationHandler}
      ref={ref => (this.map = ref)}
      >
      {places.map((place, index) => {
      <MapView.Marker
      coordinate={place.coordinates}
      title={place.title}
      />;
      })}
      {marker}
      </MapView>
      </View>
      );
      }
      }

      export default Parking;

      const styles = StyleSheet.create({
      container: {
      width: "100%",
      alignItems: "center",
      paddingBottom: 10,
      paddingLeft: 10,
      paddingRight: 10,
      paddingTop: 10
      // backgroundColor:"#192f6a"
      },
      map: {
      height: "100%",
      width: "100%"
      },
      button: {
      margin: 8
      }
      });


      But It is showing Nothing On the Map. When I console log the places Like this.



      if (places) {
      this.setState({ placesList: places });
      console.log(places);
      }


      It shows an Empty array. If I console log the placesList inside the fetch it shows the results. Can Someone help me to solve this problem and To modify My code in order to show the markers for the places that I have got from the fetch result from google API in the map?. Thank You very Much!!.







      react-native google-api react-native-maps






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 at 2:03









      dwp

      10910




      10910
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I'm fairly new to all this but I would say a couple of things:



          1 - You're declaring 'place' as a const const place = ... but you're also trying to update it within the map loop, so I'm guessing that won't work. Use var place = ... instead?



          2 - Instead of places[index] = place, does places.push(place) work?






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357273%2fcreating-a-data-and-pushing-in-to-array-and-showing-them-in-a-map-is-not-working%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            I'm fairly new to all this but I would say a couple of things:



            1 - You're declaring 'place' as a const const place = ... but you're also trying to update it within the map loop, so I'm guessing that won't work. Use var place = ... instead?



            2 - Instead of places[index] = place, does places.push(place) work?






            share|improve this answer

























              up vote
              0
              down vote













              I'm fairly new to all this but I would say a couple of things:



              1 - You're declaring 'place' as a const const place = ... but you're also trying to update it within the map loop, so I'm guessing that won't work. Use var place = ... instead?



              2 - Instead of places[index] = place, does places.push(place) work?






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I'm fairly new to all this but I would say a couple of things:



                1 - You're declaring 'place' as a const const place = ... but you're also trying to update it within the map loop, so I'm guessing that won't work. Use var place = ... instead?



                2 - Instead of places[index] = place, does places.push(place) work?






                share|improve this answer












                I'm fairly new to all this but I would say a couple of things:



                1 - You're declaring 'place' as a const const place = ... but you're also trying to update it within the map loop, so I'm guessing that won't work. Use var place = ... instead?



                2 - Instead of places[index] = place, does places.push(place) work?







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 18 at 4:46









                Andy Lower

                212




                212






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357273%2fcreating-a-data-and-pushing-in-to-array-and-showing-them-in-a-map-is-not-working%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin