从用户当前位置计算里程

时间:2018-05-01 22:11:49

标签: reactjs react-native react-native-android

在我的JSON文件中,我有目的地的纬度和经度。无论如何,我可以计算距用户当前位置的英里距离。 到目前为止,我有以下代码。我可以使用以下代码查看地图,但我无法从用户当前位置查看里程并从用户当前位置查看地图。我不知道用户当前位置的经度和纬度。我所知道的只是目的地经度和纬度。下面的代码只显示了该位置的经度和纬度所在的位置。

 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */
import React, { Component } from 'react';
import { Text, View, StyleSheet, Image, ScrollView, TouchableOpacity, Linking, Button } from 'react-native';
import { connect } from 'react-redux';
import { getTheme } from 'react-native-material-kit';
import EvilIcon from 'react-native-vector-icons/EvilIcons';
import MaterialIcon from 'react-native-vector-icons/MaterialIcons';
import SimpleIcon from 'react-native-vector-icons/SimpleLineIcons';
import * as actions from '../actions';
import getDirections from 'react-native-google-maps-directions'
const theme = getTheme();

const styles = StyleSheet.create({
  card: {
    marginTop: 10,
    paddingBottom: 20,
    marginBottom: 20,
    borderColor: 'lightgrey',
    borderWidth: 0.5,
  },

  container: {     
    flex: 1, 
    justifyContent: 'center', 
    alignItems: 'center', 
    backgroundColor: '#4F6D7A', 
    height: 500,
    alignSelf:'center',
    width:500,
    position: 'relative',
    marginTop: 5,
    top: 10
  }, 

  title1: {
      top: 10,
      left: 80,
      fontSize: 24,
  },
  title2: {
      top: 35,
      left: 82,
      fontSize: 18,
  },
  image: {
      flex: 0,
      height: 100,
      width: 333,
      backgroundColor: 'transparent',
      justifyContent: 'center',
      alignItems: 'center',
  },
  closeIcon: {
      position: 'absolute',
      top: 5,
      left: 295,
      color: 'black'

  },  
  icon: {
      position: 'absolute',
      top: 15,
      left: 0

  },
  textArea: {
      flexDirection: 'row',
      paddingLeft: 20,
      paddingTop: 10,
      width: 260,
  },
  textIcons: {
      color: '#26a69a',
  },
  actionArea: {
      paddingTop: 10,
      flexDirection: 'row',
      justifyContent: 'space-around',
      alignItems: 'center',
  },

  title:{
    justifyContent: 'center',
    paddingTop: 10,
    alignItems: 'center',
    alignSelf: 'center',
    fontWeight: 'bold',
    fontSize: 22,
    color: 'black'
    },

    SerContent:{
        fontWeight: 'bold',
        fontSize: 16,
        paddingTop: 10,
        alignSelf: 'center',
        color: 'black'
    },

    underLineText: {
        fontSize: 17,
        textDecorationLine: 'underline',
        color: 'black',
        fontWeight: 'bold',
        textAlign: 'center',

      },

      dir:{
        flexDirection:'row',
        paddingTop: 30,
        textAlign: 'center',
        fontSize: 17,
        alignSelf: 'center'

    } ,   

    Address1:{
        alignSelf: 'center',
        marginRight: 20,
        fontSize: 17,
        fontWeight: 'bold',
        color: 'black'
    },
    toolbar:{
        flexDirection:'row' ,   //Step 1

    },
    toolbarTitle:{
        width: 10,
        top: 0,
        left: 0,
        bottom: 0,
        right: 0,
        flex:1                //Step 3
      },
      buttonShape:{

        margin: 40,
        width:160,
        marginLeft: 80,
        backgroundColor:'#00BCD4',


    },

});
class ServiceDetail extends Component {




  render() {




var destUrl = 'https://www.google.com/maps/search/?api=1&query=' + this.props.services.destAddr1 + '+' + 'field'

return (



     <View>

         <View>

             <Text  style={styles.title}>{this.props.services.ser}</Text>
            <Text style={styles.SerContent} >Service is available in the following locations:</Text>
        </View>

         <View style={styles.dir}>
                <Text style={styles.Address1}> {this.props.services.Location}:</Text>   
                <TouchableOpacity  onPress={() => Linking.openURL(destUrl)}>
                    <Text style={styles.underLineText}>Directions</Text>
                </TouchableOpacity>
        </View>


   </View>



  );
}
}



const mapStateToProps = state => {
    return { 
        services: state.serviceSelected
     };
  };



export default connect(mapStateToProps, actions)(ServiceDetail);

以下是我的JSON文件

[
{
    "id":1,
    "ser": "Service1",
    "Location": "TestLoc1",
    "Phone":"(999)-921-9292",
    "SecondLoc": "TestLoc",
    "email":"test1@test.com",
    "sourceLat":"33.977806",
    "sourceLong":"-117.3732541",
    "destLatL1":"33.613355",
    "destLongL1":"-114.596569",
    "destLatL2":"33.761693",
    "destLongL2":"-116.971169",
    "destAddr1": "Test Drive, 99999",
    "destAddr2": "Test City, Test Drive, 92345"
  },

  {
    "id":1,
    "ser": "Service2",
    "Location": "TestLoc1",
    "Phone":"(999)-921-9292",
    "SecondLoc": "TestLoc",
    "email":"test2@test.com",
    "sourceLat":"33.977806",
    "sourceLong":"-117.373261",
    "destLatL1":"33.613355",
    "destLongL1":"-114.596569",
    "destLatL2":"33.761693",
    "destLongL2":"-116.971169",
    "destAddr1": "Test Drive, 99999",
    "destAddr2": "Test City, Test Drive, 92345"
  },
  ]

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

如果您不知道用户的当前位置,则无法计算到给定目标位置的距离。您写道,Google地图知道您当前的位置,而不会真正将您的位置放入其中。这是因为Google地图会检查手机的GPS。

你也可以这样做! React Native附带了一个Geolocation API,可帮助您检索用户的当前位置。对于Android,您需要更新AndroidManifest.xml以获取用户访问GPS数据的权限。对于iOS,它默认启用,但是如果您想在应用程序在后台运行时获取访问权限;您需要更新Info.plist

有关获取权限和使用Geolocation API的更多信息,请查看React Native docs

答案 1 :(得分:-1)

我用谷歌搜索,发现这是获取当前位置的方法。我只需要将起始地址设置为Current+Location。我不需要经度和纬度,这就是我想要的。

https://maps.google.com?saddr=Current+Location&daddr=43.12345,-76.12345