博览会-需要帮助在API位置数据周围设置半径

时间:2019-03-29 03:27:46

标签: api react-native geolocation location expo

尝试将当前位置的半径设置为具有位置的API。 目前,新的EXPO SDK现在已经启动了地理围栏,并使用taskManager来管理请求。

这是一个概念证明,新SDK是否确实可以满足要求。

可以获取当前位置。 能够显示API中的所有数据(包括经度和纬度)

现在,让当前位置仅显示从API到特定半径的位置。

目前,我不需要运行后台任务并更新当前位置-只需在一个位置然后从API提取半径内的位置即可。

任何帮助或在线教程都将提供帮助。

在EXPO快餐中运行此程序以证明概念。

由于地理围栏选项对于SDK来说是非常新的-在线教程不可用,并且通过大量搜索,大多数用户都无法获得最终结果。

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

TaskManager.defineTask('geoTask', ({ data: { eventType, region }, error }) => {
  if (error) {
    console.log("geoTaskError", {error});
    return;
  }
  if (eventType === Location.GeofencingEventType.Enter) {
    console.log("geoTaskEnter", { eventType, region });
  } 
  else if (eventType === Location.GeofencingEventType.Exit) {
    console.log("geoTaskExit", { eventType, region });
  }
});


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({ locationLat: (location.coords.latitude) });
    this.setState({ locationLon: (location.coords.longitude) });
    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 }});
  };

  _startGeofencingAsync = async (geoTask, regions) => {
  this.setState ({ regions: {radius: 4000}})
  };

  renderItem = ({item}) => {
    return (
    <View>
      <View>
        <Text>CODE: {item.code}
        <Text> AIRPORT NAME: {item.name}
        <Text> LAT: {item.lat}
        <Text> LONG: {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)    
    });

    let onPress = async () => {
    await Location.startLocationUpdatesAsync('geoTask', {
      accuracy: Location.Accuracy.Balanced,
    });
  };
  }

  render() {
    return (
      <View style={styles.container}>

        <Text>Latitude: {this.state.locationLat}</Text>
        <Text>Longitude: {this.state.locationLon}</Text>
        <Text>Location: {this.state.locationResult}</Text>

      <View style={styles.container}>
        <FlatList
          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',
  },
});

当前位置->具有半径->地理围栏从API提取半径内的周围位置

0 个答案:

没有答案