Взаимодействовать с маркером с местоположением пользователя - PullRequest
0 голосов
/ 10 мая 2019

Я реализовал react-native-maps с Expo, и я хочу создать небольшую игру.У меня есть местоположение пользователя, и я хочу создать событие, например;если я с 5 метров маркера, я хочу что-то сделать.

Я не знаю, возможно ли это, я думаю, что есть что-то, что связано с широтой и длиной пользователя с широтой и длиной маркера ... Вот мой код:

import React, { Component } from 'react';
import {
    Text,
    View
} from 'react-native';
import { MapView,Location,Permissions } from 'expo';

export default class Game extends Component {

static navigationOptions = {
    title: 'Map',
};

constructor(props){
    super(props)
    this.state = {
        locationResult:{ latitude: 0, longitude: 0},
        errorMessage: null,
        hasLocationPermissions: false,
        region: null,
        location:null
    }
}

  _OnDragEnd = result =>{
    this.setState({locationResult: result.coordinate})
    console.log(this.state.locationResult)
    console.log(result.coordinate)
    if (result.coordinate == this.state.locationResult){
      console.log('ok')
    }else{
      console.log('pas ok')
    }
  }

  componentDidMount() {
    this._getLocationAsync();
  }

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

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

    let location = await Location.getCurrentPositionAsync({});
    this.setState({
      region: { 
        latitude: location.coords.latitude, 
        longitude: location.coords.longitude, 
        latitudeDelta: 0.0922 / 10, 
        longitudeDelta: 0.0421 / 10 },
      locationResult:{
        latitude: location.coords.latitude,
        longitude: location.coords.longitude},
      location
    });
  };

  _reachSpawn = async () => {

  }

  mapStyle = require('./mapStyle.json');

  render() {
    return (
      <View style={{flex:1}}>  
        <Text style={{height:40,backgroundColor: '#6AC4ED'}}>Score: </Text>
        <MapView
          showsUserLocation
          followsUserLocation
          style={{flex:1}}
          region={this.state.region}
          zoomEnabled={false}
          pitchEnabled={false}
          scrollEnabled={false}
          customMapStyle={this.mapStyle}
          provider={MapView.PROVIDER_GOOGLE}
          rotateEnabled={false}
          showsCompass={true}
        >
        {/* <MapView.Marker
          //image = '../../../assets/DarkSamus.png'
          coordinate={this.state.locationResult}
            title="My Marker"
            description="Some description"
            draggable
            onDragEnd={e => this._OnDragEnd(e.nativeEvent)}
            image={require('../../../assets/soldier-6.png')}
        /> */}
      {
        SPAWNS.map((m, i) =>
          <MapView.Marker
            coordinate={m.latLong}
            title={m.title}
            description={m.description}
            key={`marker-${i}`}
            pinColor= '#20794C'
            image={require('../../../assets/zombie-4.png')}
          />
        )
      }
        </MapView>
      </View>
      );
  }
}

const SPAWNS = [
  {
    key: 'Zombie',
    title: 'Zombie Level 1',
    description: 'Kill him to get 1 point',
    latLong: {
      latitude: 48.78356518226211,
      longitude: 2.3951343385137105,
    },
  },
  {
    key: 'Zombie',
    title: 'Zombie Level 1',
    description: 'Kill him to get 1 point',
    latLong: {
      latitude: 48.869702,
      longitude: 2.335888,
    },
  },
  {
    key: 'Zombie',
    title: 'Zombie Level 1',
    description: 'Kill him to get 1 point',
    latLong: {
      latitude: 48.77993070617117,
      longitude: 2.3956984115292626,
    },
  },
  {
    key: 'Zombie',
    title: 'Zombie Level 1',
    description: 'Kill him to get 1 point',
    latLong: {
      latitude: 48.780455052145385,
      longitude: 2.4131448702847615,
    },
  },
];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...