【问题标题】:How to dsplay coordinates of GPS in a Mapview?如何在 Mapview 中显示 GPS 坐标?
【发布时间】:2019-04-19 07:51:29
【问题描述】:

我需要您的帮助来了解如何在 MapView 中显示 GPS 坐标。

我想通过 gps 坐标(经度和纬度)跟踪一个人的位置。

为了恢复 MapView 中的坐标,我尝试了太多,但我没有到达,我被阻止了。

所以,我从 Web 服务获取坐标并将它们显示在 TextView 中。

这是我的代码:

import React, { Component } from 'react';
import { Constants, Location, Permissions } from 'expo';
import { Card } from 'react-native-paper';
import { Polyline } from 'react-native-maps';
import {
  StyleSheet,
  Platform,
  View,
  ActivityIndicator,
  FlatList,
  Text,
  Image,
  TouchableOpacity,
  Alert,
  YellowBox,
  AppRegistry,
  Button,
} from 'react-native';
export default class gps extends Component {
   static navigationOptions = ({ navigation }) => {
    return {
      title: 'Suivi GPS',
      headerStyle: {
        backgroundColor: 'transparent',
      },
      headerTitleStyle: {
        fontWeight: 'bold',
        color: '#000',
        zIndex: 1,
        fontSize: 18,
        lineHeight: 25,
        fontFamily: 'monospace',
      },
    };
  }
  state = {
    mapRegion: null,
    dat: '',
  };
GetItem() {}
  componentDidMount() {
    this.webCall();
  }
  FlatListItemSeparator = () => {
    return (
      <View
        style={{
          height: 0.5,
          width: '100%',
          backgroundColor: '#000',
        }}
      />
    ); //
  };
  webCall = () => {
    return fetch('http://first-ontheweb.com/onLineSenior/pos.php')
      .then(response => response.json())
      .then(responseJson => {
        this.setState({
          isLoading: false,
          dataSource: responseJson,
        });
      })
      .catch(error => {
        console.error(error);
      });
  };
  render() {
    if (this.state.isLoading) {
      return (
        <View
          style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
          <ActivityIndicator size="large" />
        </View>
      );
    }
return (
  <View style={styles.map}>  
    <FlatList
      data={this.state.dataSource}
      ItemSeparatorComponent={this.FlatListItemSeparator}
      renderItem={({ item }) => (
        <View>
          <Text style={styles.textView}>Longitude :</Text>
          <Text style={styles.textView}>{item.longitude}</Text>
          <Text style={styles.textView}>Latitude :</Text>
          <Text style={styles.textView}>{item.latitude}</Text>
        </View>
      )}
      keyExtractor={(index) => index.toString()}
    />
  </View>
);
  }
}
const styles = StyleSheet.create({
  map: {
    ...StyleSheet.absoluteFillObject,
  },
});

所以,我想在MapView 中显示 GPS 坐标。

谢谢。

【问题讨论】:

    标签: react-native react-native-maps


    【解决方案1】:

    你可以这样做:

    首先导入mapView:

    import MapView from "react-native-maps";
    

    然后:

    state = {
        focusedLocation: {
          latitude: 37.7900352, //or your coordinates
          longitude: -122.4013726, //or your coordinates
          latitudeDelta: 0.0122,
          longitudeDelta:
            Dimensions.get("window").width /
            Dimensions.get("window").height *
            0.0122
        }
      };
    
      render() {
        let marker = null;
    
        if (this.state.locationChosen) {
          marker = <MapView.Marker coordinate={this.state.focusedLocation} />;
        }
    
        return (
          let marker = <MapView.Marker coordinate={this.state.focusedLocation} />;
          <View style={styles.container}>
            <MapView
              initialRegion={this.state.focusedLocation}
              style={//map style here}
            >
              {marker}
            </MapView>
          </View>
        );
      }
    

    【讨论】:

      【解决方案2】:

      你需要安装 react-native-maps 包而不是

      import MapView from 'react-native-maps';
      

      使用初始区域渲染地图

      <MapView
          initialRegion={{
            latitude: 37.78825,
            longitude: -122.4324,
            latitudeDelta: 0.0922,
            longitudeDelta: 0.0421,
          }}
        />
      

      在将区域控制为状态时使用 MapView

      getInitialState() {
        return {
          region: {
            latitude: 37.78825,
            longitude: -122.4324,
            latitudeDelta: 0.0922,
            longitudeDelta: 0.0421,
          },
        };
      }
      
      onRegionChange(region) {
        this.setState({ region });
      }
      
      render() {
        return (
          <MapView
            region={this.state.region}
            onRegionChange={this.onRegionChange}
          />
        );
      }
      

      在地图上呈现标记列表

      import { Marker } from 'react-native-maps';
      
      <MapView
        region={this.state.region}
        onRegionChange={this.onRegionChange}
      >
        {this.state.markers.map(marker => (
          <Marker
            coordinate={marker.latlng}
            title={marker.title}
            description={marker.description}
          />
        ))}
      </MapView>
      

      您可以在此处的文档中找到更多信息。

      https://github.com/react-native-community/react-native-maps
      

      【讨论】:

      • 如果我在 firebase db 中保存了包含 lat,long 的用户数据,并且我想在基于附近用户的地图中显示它们,如何处理?
      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多