如何在React Native的MapView中设置标记

时间:2015-04-02 12:15:39

标签: javascript google-maps reactjs react-native

  • 我想在React Native中设置MapView上的标记,但我无法通过MapView的官方文档找到任何信息。
  • 如果不允许这样做,我怎样才能在React Native中使用现有的反应模块,例如react-googlemaps

9 个答案:

答案 0 :(得分:25)

谢谢@naoufal。 最后,我能够在地图上显示标记以反应原生IOS。

<View style={styles.container}>
        <MapView style={styles.map}
          initialRegion={{
              latitude: 37.78825,
              longitude: -122.4324,
              latitudeDelta: 0.0,
              longitudeDelta: 0.0,
          }}
        >
        <MapView.Marker
            coordinate={{latitude: 37.78825,
            longitude: -122.4324}}
            title={"title"}
            description={"description"}
         />
      </MapView>
 </View>

对于地图和容器样式,我使用了以下样式:

 var styles = StyleSheet.create({

  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
    map: {
      position: 'absolute',
      top: 0,
      left: 0,
      right: 0,
      bottom: 0,
    }
});

我参考了以下链接: React native IOS- Display Markers on map

答案 1 :(得分:24)

您可以使用annotations prop.

设置标记

例如:

var markers = [
  {
    latitude: 45.65,
    longitude: -78.90,
    title: 'Foo Place',
    subtitle: '1234 Foo Drive'
  }
];

<MapView
  region={...}
  annotations={markers}
/>

答案 2 :(得分:2)

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

<View>
    <MapView
    style={styles.mapStyle}
    initialRegion={{
    latitude: 37.78825,
    longitude: -122.4324,
    latitudeDelta: 0.0922,
    longitudeDelta: 0.0421,
    }}
    >
        <Marker coordinate = {{latitude: 37.78825,longitude: -122.4324}}
         pinColor = {"purple"} // any color
         title={"title"}
         description={"description"}/>
    </MapView>
</View>

答案 3 :(得分:1)

根据this issue,它还没有曝光。您无法使用Web-React软件包,React Native也不会像这样工作。

两个建议:

  1. 等待上述问题得到解决以获得正确的API
  2. 尝试使用WebView链接到带有注释的Google地图
  3. 我确实不确定#2是否可行。

答案 4 :(得分:1)

谢谢@zia_qureshi。最后,我能够在地图上显示标记以响应本机android。

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

const App = () => {
  const [region, setRegion] = useState({
    latitude: 51.5078788,
    longitude: -0.0877321,
    latitudeDelta: 0.009,
    longitudeDelta: 0.009
  });

  return (
    <MapView
      style={{ flex: 1 }}
      region={region}
      onRegionChangeComplete={region => setRegion(region)}
    >
      <Marker coordinate={{ latitude: 51.5078788, longitude: -0.0877321 }} />
    </MapView>
  );
};

export default App;

答案 5 :(得分:1)

更新的代码-2020 |反应原生0.63

import MapView, { Marker } from "react-native-maps";
<MapView
    style={styles.mapStyle}
    region={{
        latitude: this.state.mapLat,
        longitude: this.state.mapLong,
        latitudeDelta: 0.001663,
        longitudeDelta: 0.002001,
            }}
        onRegionChangeComplete={this.onRegionChange}
>
    <Marker
        coordinate={{latitude: 51.5078788, longitude: -0.0877321}}
    >
    </Marker>
</MapView>

答案 6 :(得分:1)

对于那些寻找动态(例如,来自 API)而不是硬编码坐标的人;这是 Expo CLI 自动生成的屏幕之一的复制/粘贴片段,包括 React Hooks

import axios from "axios";
import MapView from "react-native-maps";
import { Dimensions, StyleSheet } from "react-native";
import { Marker } from "react-native-maps";
import { Text, View } from "../components/Themed";

export default function TabTwoScreen() {
  const [markers, setMarkers] = useState(null);

  useEffect(() => {
    axios({
      method: "GET",
      url: "https://API_URL...",
      headers: {
        Authorization: `Bearer MY_TOKEN_ABC123...`,
        Accept: "application/json",
      },
    })
      .then((response) => {
        setMarkers(response.data.results);
      })
      .catch((error) => {
        console.log(error);
      });
  }, []); // "[]" makes sure the effect will run only once.

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Tab Two</Text>
      <MapView style={styles.map}>
        {markers &&
          markers.map((marker: any, index: number) => (
            <Marker
              key={index}
              coordinate={{
                latitude: marker.location[1],
                longitude: marker.location[0],
              }}
              title={marker.title}
              description={marker.description}
            />
          ))}
      </MapView>
    </View>
  );
}

const styles = StyleSheet.create({
  // Add your styles here...
});

答案 7 :(得分:0)

import MapView from 'react-native-maps';
<View style={StyleSheet.absoluteFillObject}>
        <MapView style={styles.map}
          showsUserLocation //to show user current location when given access
          loadingEnabled //to show loading while map loading
          style={styles.map}
          initialRegion={{
              latitude,
              longitude,
              latitudeDelta: 0.0922,
              longitudeDelta: 0.0421,
          }}
        >
        {

             locations && locations.map((location, index) => {
                   const {
                       coords: { latitude, longitude }
                          } = location;
                                return (
                                    <MapView.Marker
                                        key={index}
                                        coordinate={{ latitude, longitude }}
                                        title={"title"}
                                        description={"address"}
                                        // onPress={this.onMarkerPress(location)}
                                    />
                                )
                            })
                        }
      </MapView>
 </View>```

const styles = StyleSheet.create({

map: {
  position: 'absolute',
  top: 0,
  left: 0,
  right: 0,
  bottom: 0,
}

});


“依赖项”:{     “ native-base”:“ ^ 2.13.8”,     “ react”:“ 16.9.0”,     “ react-native”:“ 0.61.2”,     “ react-native-maps”:“ git + https://git@github.com/react-native-community/react-native-maps.git”   }


Try using this. Hope it helps to mark for many locations. Happy Coding.

答案 8 :(得分:-2)

您可以尝试使用webview -

<project 
  xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <name>ews-api</name>
  <groupId>net.retn</groupId>
  <artifactId>ews-api</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <properties>
    <encoding>UTF-8</encoding>
    <cxf.version>3.1.6</cxf.version>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-codegen-plugin</artifactId>
        <version>${cxf.version}</version>
        <dependencies>
          <dependency>
              <groupId>net.retn</groupId>
              <artifactId>cxf-ews-authenticator</artifactId>
              <version>1.0-SNAPSHOT</version>
          </dependency>
        </dependencies>
        <executions>
          <execution>
              <id>generate-sources</id>
              <phase>generate-sources</phase>
              <configuration>
                <sourceRoot>${project.build.sourceDirectory}/</sourceRoot>
                <wsdlOptions>
                  <wsdlOption>
                    <wsdl>https://server/EWS/Services.wsdl</wsdl>
                    <extraargs>
                      <extraarg>-b</extraarg>
                      <extraarg>${basedir}/src/main/resources/async_binding.xml</extraarg>
                    </extraargs>
                  </wsdlOption>
                </wsdlOptions>
              </configuration>
              <goals>
                <goal>wsdl2java</goal>
              </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

可以使用标记和其他选项。 希望它会有所帮助!