开发者

offline gps coordinate access - iPhone/Android

I'm working on a little app, and one of our requirements is to check a users physical location when he did a particular activity (Example: A picture must be taken near Eiffel Tower only)

Now, a lot of our smartphone users might or might not have Data Access at that very instant.

Is it possible to fetch the users Latitude and Longitude when he's offline (No 3G/WiFi).???

I guess the Maps app on iPhone uses data connectivity just to render the underlying G开发者_StackOverflowoogle Maps and the iPhone OS still has access to device's GPS co-ordinates. Please let me know if i'm right or wrong..

I would like to learn the same about other OS'es, example: Android. Any links/references will be much appreciated.

UPDATE: I'm aware that GPS is a satellite based technology and hence i guess it should have no direct relation with internet connectivity.


You are right! To get the latitude and longitude Informations the devices use the GPS hardware. This is done without internet connection. What you need is a GPS signal! However, if you are inside buildings where you often don't have good GPS signals the iPhone tries to also locate the device using surrounding 3G and WIFI nets.

If you want to display that coordinates for example with google maps you need internet connection to load the maps according to the coordinates you got. Also, to retrieve informations like address,street, etc. the devices needs access to the internet to search the databases for this kind of informations.

EXAMPLE for iOS:

Fist import #import<CoreLocation/CoreLocation.h> to you .h file.

Then basically you need the CLLocationManager.

- (IBAction)startSearchingLocation:(id)sender
{
    //locationManager is ivar of CLLocationManager
    self.locationManager = [[CLLocationManager alloc] init];
    //release it when you stop the location manager!

    // make self the delegate of the location manager
    [locationManager setDelegate:self];

    //Note that this settings will have a huge impact on battery life! 
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

    //And finally start the location manager.
    [locationManager startUpdatingLocation];
}

The location Manager will start sending location informations to a delegate method called:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

It's recommended to throw the very first results away because they could be old cached data. Also check for accuracy to make sure you get good results here.

REFERENCES:

For more informations check out this and this documentation.

Also take a look at the apple example code named Locations.


thats code works with android

private double[] getGPS() {
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
        List<String> providers = lm.getProviders(false);

        /* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
        Location l = null;

        for (int i=providers.size()-1; i>=0; i--) {
                l = lm.getLastKnownLocation(providers.get(i));
                if (l != null) break;
        }

        double[] gps = new double[2];
        if (l != null) {
                gps[0] = l.getLatitude();
                gps[1] = l.getLongitude();
        }
        return gps;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜