博览会-使用API​​数据响应本机GeoFence

时间:2019-05-02 01:31:41

标签: api react-native geolocation expo

我正在尝试为围绕某些经度和纬度进行地理围栏的简单概念证明

结构:-开启位置服务(现在不需要后台更新,只需初始位置即可)-确定了用户位置-查看json文件中的位置数据并在@ 10km附近找到位置(请参考URL API)

EXPO最近更新了SDK来进行管理,但是通过多次搜索,真正实现了将静态位置放置到数据库然后运行接近半径的简单任务。

使用该概念的基本设置,但不确定下一步...下面是我现在所拥有的设置,并希望了解GeoFence调用开始的方向

import React, {Component} from 'react';
    import { AppRegistry, Text, View, StyleSheet, FlatList, Image } from 'react-native';
    import { Constants, MapView, Location, Permissions } from 'expo';

export default class App extends Component {
  constructor() {
    super ()
    this.state = {
      dataSource: []
    }
  }

  state = {
    mapRegion: null,
    hasLocationPermissions: false,
    locationResult: null
  };

  _handleMapRegionChange = mapRegion => {
    console.log(mapRegion);
    this.setState({ mapRegion });
  };

  _getLocationAsync = async () => {
   let { status } = await Permissions.askAsync(Permissions.LOCATION);
   if (status !== 'granted') {
     this.setState({
       locationResult: 'Permission to access location was denied',
     });
   } else {
     this.setState({ hasLocationPermissions: true });
   }

   let location = await Location.getCurrentPositionAsync({});
   this.setState({ locationResult: JSON.stringify(location) });

   // Center the map on the location we just fetched.
    this.setState({mapRegion: { latitude: location.coords.latitude, longitude: location.coords.longitude, latitudeDelta: 0.0922, longitudeDelta: 0.0421 }});
  };

  renderItem = ({item}) => {
    return (
    <View>
      <View>
        <Text>{item.code}  
        <Text>{item.name} 
        <Text>{item.lat} 
        <Text>{item.lon} 
        </Text>
        </Text>
        </Text>
        </Text>
      </View>
    </View>
  )}

  componentDidMount() {
    this._getLocationAsync();
    //const url = 'https://www.json-generator.com/api/json/get/ccLAsEcOSq?indent=1'
    const url = 'https://gist.githubusercontent.com/tdreyno/4278655/raw/7b0762c09b519f40397e4c3e100b097d861f5588/airports.json'
    fetch(url)
    .then((repsonse) => repsonse.json())
    .then((responseJson) => {
      this.setState({
        dataSource: responseJson
      })
    })
    .catch((error) => {
        console.log(error)
    })
  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.paragraph}>
          Pan, zoom, and tap on the map!
        </Text>

        {
          this.state.locationResult === null ?
          <Text>Finding your current location...</Text> :
          this.state.hasLocationPermissions === false ?
            <Text>Location permissions are not granted.</Text> :
            this.state.mapRegion === null ?
            <Text>Map region doesn't exist.</Text> :
            <MapView
              style={{ alignSelf: 'stretch', height: 200 }}
              region={this.state.mapRegion}
              onRegionChange={this._handleMapRegionChange}
            />
        }

        <Text>
          Location: {this.state.locationResult}
        </Text>

      <View style={styles.container}>
        <FlatList
          //={[{ key: 'a'}, { key: 'b'}]}
          //renderItem={({item }) => <Text>{item.key}</Text>}
          data={this.state.dataSource}
          renderItem={this.renderItem}
          />
      </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  paragraph: {
    margin: 24,
    fontSize: 18,
    fontWeight: 'bold',
    textAlign: 'center',
  },
});

这仅是概念证明,仅需要一些指导即可帮助我继续前进。链接和教程对您有帮助

感谢(仍在学习中)

0 个答案:

没有答案
相关问题