user2549922
user2549922

Reputation: 39

How to find Geolocation (longitude and latitude)in jquery mobile +phonegap

can you please tell me how to find Geo location (longitude and latitude) in jquery mobile using phonegap.And then i need to show the position on map.

Upvotes: 0

Views: 4020

Answers (1)

Amol Chakane
Amol Chakane

Reputation: 1511

I assume you have referred Phonegap's Getting Started Guide to create basic project.

This is the script to get Lattitude and Longitude

/*
 * This file contains script to get lattitude and longitude
 * 
 */
var lat = 0;
var lng = 0;
//A button click will call this function
function getLocation() {
    navigator.geolocation.getCurrentPosition(onSuccess, onError, { enableHighAccuracy: true });
}

// onSuccess Geolocation
//
function onSuccess(position) {
    //Lat long will be fetched and stored in session variables
    //These variables will be used while storing data in local database 
    lat = position.coords.latitude;
    lng = position.coords.longitude;
    alert('Lattitude: ' + lat + ' Longitude: ' + long);
    sessionStorage.setItem('lattitude', lat);
    sessionStorage.setItem('longitude', lng);
}
// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: ' + error.code + '\n' +
          'message: ' + error.message + '\n');
}

You will have to add necessary permissions in your manifest file.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

Call getLocation() function onclick event of button or wherever you wish to get geolocation.

For more details refer Phonegap Geolocation Documentation

To show google map please refer Google MAP API

Hope that helps.

Upvotes: 1

Related Questions