Created
November 9, 2017 15:23
-
-
Save andigu/c68f7083148264c1546b4914dfaf9461 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GeoLocationModule extends ReactContextBaseJavaModule { | |
public GeoLocationModule(ReactApplicationContext reactContext) { | |
super(reactContext); | |
BroadcastReceiver geoLocationReceiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Location message = intent.getParcelableExtra("message"); | |
GeoLocationModule.this.sendEvent(message); | |
} | |
}; | |
LocalBroadcastManager.getInstance(getReactApplicationContext()).registerReceiver(geoLocationReceiver, new IntentFilter("GeoLocationUpdate")); | |
} | |
@Override | |
public String getName() { | |
return "GeoLocation"; | |
} | |
@ReactMethod | |
public void startService(Promise promise) { | |
String result = "Success"; | |
try { | |
Intent intent = new Intent(GeoLocationService.FOREGROUND); | |
intent.setClass(this.getReactApplicationContext(), GeoLocationService.class); | |
getReactApplicationContext().startService(intent); | |
} catch (Exception e) { | |
promise.reject(e); | |
return; | |
} | |
promise.resolve(result); | |
} | |
@ReactMethod | |
public void stopService(Promise promise) { | |
String result = "Success"; | |
try { | |
Intent intent = new Intent(GeoLocationService.FOREGROUND); | |
intent.setClass(this.getReactApplicationContext(), GeoLocationService.class); | |
this.getReactApplicationContext().stopService(intent); | |
} catch (Exception e) { | |
promise.reject(e); | |
return; | |
} | |
promise.resolve(result); | |
} | |
private void sendEvent(Location message) { | |
WritableMap map = Arguments.createMap(); | |
WritableMap coordMap = Arguments.createMap(); | |
coordMap.putDouble("latitude", message.getLatitude()); | |
coordMap.putDouble("longitude", message.getLongitude()); | |
coordMap.putDouble("accuracy", message.getAccuracy()); | |
coordMap.putDouble("altitude", message.getAltitude()); | |
coordMap.putDouble("heading", message.getBearing()); | |
coordMap.putDouble("speed", message.getSpeed()); | |
map.putMap("coords", coordMap); | |
map.putDouble("timestamp", message.getTime()); | |
getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("updateLocation", map); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment