Teerath Kumar
Teerath Kumar

Reputation: 488

Get gps location using javascript

I am using tablet, working on html application, want to get gps location without using any network connection. please suggest me simple ways.

Upvotes: 3

Views: 5581

Answers (4)

Kamesh
Kamesh

Reputation: 1465

You have to create a instance of LocationManager class. and You can use getLastKnownLocation method to find the location. you must pass Location.GPS_PROVIDER as an argument.

LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
Location userLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.d("Latitude", userLocation.getLatitude());
Log.d("Longitude", userLocation.getLongitude());

Upvotes: 0

furkan3ayraktar
furkan3ayraktar

Reputation: 543

If you want to get more accurate location feed, you should request for updates, the last known location will not be sufficient for you. GPS_PROVIDER will supply only location updates from the gps, not from network provider. However, if you use NETWORK_PROVIDER, you can get more accurate location values indoor.

public class MainActivity extends Activity implements LocationListener{

    protected LocationManager locationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d("Latitude","disable");
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d("Latitude","enable");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d("Latitude","status");
    }
}

Upvotes: 0

Md. Shahadat Sarker
Md. Shahadat Sarker

Reputation: 849

Give the permission:

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

Get the longitude & latitude:

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

Save in txt file:

public void generateLatLongOnSDCard(String sFileName, String sBody){
    try
    {
        File rootDirFile = new File(Environment.getExternalStorageDirectory(), "LatLong");
        if (!rootDirFile.exists()) {
            rootDirFile.mkdirs();
        }
        File gpxfile = new File(rootDirFile, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();
        Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
    }
    catch(IOException e)
    {
         e.printStackTrace();
    }
   }

And finally you can read the text file.

Upvotes: 0

Panther
Panther

Reputation: 9418

Using JS you can get it like this

 <script>
   var x = document.getElementById("demo");
    function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}
function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude +  "<br>Longitude: " + position.coords.longitude;
}
</script>

Upvotes: 6

Related Questions