skip to Main Content

Hey everyone so I’m learning React Native, using Expo VS code, and android studio

I was trying to get a location image with a pin using google maps.

But I cant see any map to pick my location at google maps in android studio.
Also, I cant see any marker after picking a location in iphone expo go google maps image.

What should I do?

android studio google maps

iphone expo go google maps' image after licking a location

Below is my code about getting image of map

export function getMapPreview(lat, lng) {
  const imagePreviewUrl = `https://maps.googleapis.com/maps/api/staticmap?center=${lat},${lng}&zoom=13&size=400x200&maptype=roadmap&markers=color:red%7Clabel:S%7C${lat},${lng}
  &key=${GOOGLE_API_KEY}`;

  return imagePreviewUrl;
}

Map screen

import { useCallback, useLayoutEffect, useState } from "react";
import { StyleSheet, Alert } from "react-native";
import MapView, { Marker } from "react-native-maps";

import IconButton from "../components/UI/IconButton";

export default function Map({navigation}) {
  const [selectedLocation, setSelectedLocation] = useState();

  const region = {
    latitude: 37.56,
    longitude: 126.97,
    latitudeDelta: 0.0922,
    longitudeDelta: 0.0421
  }

  function selectLocationHandler(event) {
    console.log(event);
    const lat = event.nativeEvent.coordinate.latitude;
    const lng = event.nativeEvent.coordinate.longitude;

    setSelectedLocation({lat: lat, lng: lng})
  }

  const savePickedLocationHandler = useCallback(() => {
    if (!selectedLocation) {
      Alert.alert(
        "No location picked",
        'You have to pick a loacation (by tapping on the map) first!'
      )

      return;
    }

    navigation.navigate('AddPlace', { pickedLat: selectedLocation.lat, pickedLng: selectedLocation.lng })
  }, [navigation, selectedLocation])

  useLayoutEffect(() => {
    navigation.setOptions({
      headerRight: ({tintColor}) => (
        <IconButton icon="save" size={24} color={tintColor} onPress={savePickedLocationHandler} />
      )
    })
  }, [navigation, savePickedLocationHandler])

  return (
    <MapView style={styles.map} initialRegion={region} onPress={selectLocationHandler}>
      {selectedLocation &&
        <Marker
          title="Picked Location"
          coordinate={{latitude: selectedLocation.lat,longitude: selectedLocation.lng}}
        />
      }
    </MapView>
  )
}

const styles = StyleSheet.create({
  map: {
    flex: 1
  }
})

I reviewed my code several times, but I cant find why

2

Answers


  1. Try use width and height to map styles

    Login or Signup to reply.
  2. You need to define the width and height of the map.

    const styles = StyleSheet.create({
      map: {
        width: MAP_WIDTH_VALUE,
        height:MAP_HEIGHT_VALUE
      }
    })
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search