Thursday 26 July 2012

Phonegap get Latitude and Longitude / PhoneGap Geolocation Sample Application

Hello Friends,

With the help Phone Gap Api, You can easily get current position using GPS .
To start playing with it, Please have a look at this simple function or Code:

// Check if geolocation is available
if(navigator.geolocation) {
    // This is the specific PhoneGap API call
    navigator.geolocation.getCurrentPosition(function(p) {
        // p is the object returned
        alert('Latitude '+p.coords.latitude);
        alert('Longitude '+p.coords.longitude);
    }, function(error){
        alert("Failed to get GPS location");
    });
} else {
    alert("Failed to get GPS working");
}


Also Please add permission in your plugin.xml file
1.<plugin name="Geolocation" value="com.phonegap.GeoBroker"/>
2. <plugin name="Notification" value="com.phonegap.Notification"/>



Enjoy Coding.... :)
Please, write questions in comments.
Don’t forget share this post....... :) 

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

9 comments:

  1. Hi,

    I tried this code in button click event. But not reposed an alert.
    I want to send geolocation to my server.Before that have show in alert message.

    ReplyDelete
  2. Hey hi,
    Have passed the permission in plugin.xml file for
    Geolocation and Notification. Please add it in your xml file.


    ReplyDelete
  3. I just updated the few things in this post,
    like the permission which is necessary to add inside the plugin.xml file.

    ReplyDelete
  4. where is the plugin.xml file present? i can not find any in my project!

    ReplyDelete
  5. Hello Talha,

    You have to create a xml file inside
    => res/xml/plugins.xml

    So first create an xml folder inside your res folder and the under xml folder placed your
    plugins.xml file.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. How to display markers on map using phonegap?
    I have to add multiple markers,displaying info window on click event.

    ReplyDelete
  8. I am new to phonegap, I would like to program an app where you enter the address then you get the geolocation.
    is that possible?
    hello people
    someone has an idea

    ReplyDelete

 

Copyright @ 2013 Android Developers Blog.