Break data divided by virgula
-
I am trying to make a tracker using arduino, a gps module and a gsm.
I can receive latitude, longitude and send to mobile as SMS, but on mobile I am trying to build an application with
react-native
, in which I am learning, show the location on the map.Running tests, I got this code to monitor incoming messages:
import React, { Component } from 'react'; import { AppRegistry, Text, View, Button } from 'react-native';
import SmsListener from 'react-native-android-sms-listener';
export default class App extends Component {
//constructor include last message
constructor(props) {
super(props);
this.state = { lastMessage: 1 };
}sms(){
SmsListener.addListener(message => {
this.setState({ lastMessage: message.body });
});
}render() {
return (
<View>
<Text> Scheduled jobs: {this.state.lastMessage} </Text><Button title="Buscar" color="#115E54" onPress={() => this.sms() } /> </View> );
}
}
And this code to display the map:
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View
} from 'react-native';import MapView from 'react-native-maps';
export default class App extends Component {
render() {
const { region } = this.props;
console.log(region);return ( <View style ={styles.container}> <MapView style={styles.map} initialRegion={{ latitude: 37.78825, longitude: -122.4324, latitudeDelta: 0.015, longitudeDelta: 0.0121, }} > </MapView> </View> );
}
}const styles = StyleSheet.create({
container: {
...StyleSheet.absoluteFillObject,
height: 400,
width: 400,
justifyContent: 'flex-end',
alignItems: 'center',
},
map: {
...StyleSheet.absoluteFillObject,
},
});
Both work within their functions.
The problem is that I get the data in the SMS format
37.78825,-122.4324
and I need to break this dice in two so that I can assignlatitude
andlongitude
How do I do that?
-
If I understand well you want to get the data separated by comma:
const str = "37.78825,-122.4324"; str = str.split(","); console.log('latidude', res[0]) console.log('longitude', res[1]);
Test here:
https://codepad.remoteinterview.io/ISHTNVJTFE