- March 21, 2022
- Posted by: Vikas Chowdhury
- Category: Programming
Native Bridging native objective c code with react-native code.
Steps to follow:
- Create two files ReverseGeocodingNative.h and ReverseGeocodingNative.m under your main ios project.
- Add below code in ReverseGeocodingNative.h
- Add below code in ReverseGeocodingNative.m
- Now, in the React- Native project you can call the above native code by importing NativeModules package and below code snippet.
- To fetch latitude and longitude, we have used ‘react-native-geolocation-service’.
import Geolocation from ‘react-native-geolocation- service’;
import {NativeModules} from ‘react-native’;
const { ReverseGeocodingNative } = NativeModules
Geolocation.getCurrentPosition(async(info) =>
{
let latitude =info.coords.latitude;
let longitude =info.coords.longitude;
let timestamp =info.timestamp;
console.log(“timestamp: “+timestamp)
this.setState({timestamp})
try{
if(Platform.OS===’ios’)
{
let data=await ReverseGeocodingNative.addressFromLatLon(latitude,longitude)
console.log(“latLong: “+JSON.stringify(data))
console.log(data.City+”, “+data.State)
let location =data.City+”, “+data.State;
this.setState({location})
}
}
catch(e)
{
console.log(“latLOng error: “+e)
}
}