Broadcast information?


#1

Hi guys, I’m looking for a way to do the following and was wondering whether ionic/cordova could do this.

  • a user is in a certain perimeter / geofence?
  • my app should automatically “see” that user in a list
  • I want to support at least modern versions of ios and android.

Where should I start looking for this functionality?
Can this work offline, without e.g. broadcasting geolocation x.y coordinates?

Thanks for pointing me in the right direction.


#2

You could implement this really fine, espescially on newer devices.

  1. You can get the current geolocation using ng-cordova’s implementation for this: http://ngcordova.com/ (This can happen offline)
  2. You can either create a calculation method that determines how much miles a geolocation is from the current geolocation, and determine whether this is within a given radius. If they are within the radius, you could make the user visible in the list. Biggest problem would be writing the checking algorithm (which I’m sure you’ll find pretty easily when googling) (In pure online app, you could use google map’s api’s for this as well… but you want to support offline apps)
  3. For all users that you have in memory, you could determine if you show them or not, ased on their last known location -from the apps point of view-, of course, when the server receives a new location this can’t be synced while offline.

#3

Thanks for the ngcordova link, I didn’t know about that yet, is that also an ionic effort?

Do I understand correctly that using ngcordova is the same as just calling navigator.geolocation.getCurrentPosition through javascript? Or is there something extra that ngcordova is doing in this case besides providing angular syntax?

Thanks again!


#4

This is how i get current position:

navigator.geolocation.getCurrentPosition(function(pos) {
	latlong =  { 'lat' : pos.coords.latitude, 'long' : pos.coords.longitude }
	$rootScope.$broadcast('currentLocation', { currentLocation: latlong });
	$rootScope.currentLocation = latlong;

}, function(error) {
	console.log('Got error!');
	console.log(error);
	latLong = null
	
	q.reject('Failed to Get Lat Long')

});

And then i use this to calculate the distance:

//currentlocation
var from = new google.maps.LatLng($rootScope.currentLocation.lat, $rootScope.currentLocation.long);

//value[1] contains latitude value[2] contains longitude
var to = new google.maps.LatLng(value[1],value[2]);

//calculate distance using google maps 
var dist = google.maps.geometry.spherical.computeDistanceBetween(from, to);

//distance is returned p meter
var km = dist / 1000;

//if its 1000 meters say km and round to whole else return distance tofixedwhich(0) is pretty much the same as math.round
if(km > 1){
	var dist =(Math.round(km) + " km"); 
}else {
	var dist =(dist.toFixed(0) + "m"); 
}// 1613.8 km

#5

Thanks for the sample code, I appreciate it. I’ve written several geolocation based apps in plain Javascript, but I’m confused whether org.apache.cordova.geolocation as mentioned in ngcordova provides any benefits compared to using navigator.geolocation.getCurrentPosition()

Can somebody clarify that?


#6

Looking into the module i’d say the benefits are just calling the function and getting the results without having to write the whole function(wow)

Here’s the factory

 return {
  getCurrentPosition: function (options) {
    var q = $q.defer();

    navigator.geolocation.getCurrentPosition(function (result) {
      // Do any magic you need
      q.resolve(result);
    }, function (err) {
      q.reject(err);
    }, options);

    return q.promise;
  },
  watchPosition: function (options) {
    var q = $q.defer();

    var watchId = navigator.geolocation.watchPosition(function (result) {
      // Do any magic you need
      q.notify(result);

    }, function (err) {
      q.reject(err);
    }, options);

    return {
      watchId: watchId,
      promise: q.promise
    }
  },

  clearWatch: function (watchID) {
    return navigator.geolocation.clearWatch(watchID);
  }
}

#7

@sjerd is the org.apache.cordova.geolocation something that is always required when using geolocation in cordova apps as opposed to just callling getCurrentPosition() in a browser like chrome?


#8

Yes, it seems this way. I just removed the plugin and ran my app again and it didn’t get my location, ran it again with the plugin and it works again so i’m pretty sure it’s required. maybe if you use in app browser which gives you current location but seems like alot of extra effort instead of just using the plugin.


#9

Thanks @sjerd , the plugin probably is required because it’s just a webview and not a full browser. Not sure though.


#10

The plugin (and permission) is indeed required for applications to use the device geolocation api


#11

thanks @iwantwin

Can you tell us why this is required, compared to using simple javascript in the browser to grab the current location? Does this have anything to do with the webview vs. full browser?


#12

Yeah, the webview runs inside the application, and the full browser is an application on it’s own. The full browser is actually an app with the geolocation permissions activated. For it to work, the shell for the webview (the app) needs the permissions or it cant’give one of it’s children (the webview) the same data.

If you are building a native application, you still need permission to use the geolocation api, but you could write your own code for this. The cordova plugin basically translates this geolocation api to your webview, which results in a necessity to use the plugin when building hybrid apps.

Hope it’s clear, seems kinda fuzzy as an answer to me :stuck_out_tongue:


#13

Totally clear sir. Thanks for helping out!