Breaking data divided by comma

Asked

Viewed 82 times

1

I’m trying to make a tracker using Adian, a gps module and a gsm.

I can receive the latitude, longitude and send to cell phone as SMS, but in cell phone I’m trying to build an application with react-native, in which I am learning, to show on the map the location.

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 in the fact that I receive the data in the SMS format 37.78825,-122.4324 and I need to break this data in two to be able to assign in latitude and longitude

How do I do that?

1 answer

1


If I understand correctly you want to take 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

  • Well that’s what I need, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.