get Longitude - Android Map

Android examples for Map:Latitude Longitude

Description

get Longitude

Demo Code


//package com.java2s;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class Main {
    public static double getLongitude(final Context context) {
        double longitude = 0.0;
        LocationManager locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Location location = locationManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location != null) {
                longitude = location.getLongitude();
            }/*  w w  w  .ja va 2 s  .c  om*/
        } else {
            LocationListener locationListener = new LocationListener() {

                @Override
                public void onStatusChanged(String provider, int status,
                        Bundle extras) {

                }
                @Override
                public void onProviderEnabled(String provider) {

                }

                // Provider?disable?GPS?
                @Override
                public void onProviderDisabled(String provider) {

                }

                //Provider
                @Override
                public void onLocationChanged(Location location) {
                    if (location != null) {
                        /*Log.e("Map", "Location changed : Lat: "
                                + location.getLatitude() + " Lng: "
                                + location.getLongitude());*/
                    }
                }
            };
            if (locationManager.getAllProviders().contains(
                    LocationManager.NETWORK_PROVIDER))
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, 0, 0,
                        locationListener);

            if (locationManager.getAllProviders().contains(
                    LocationManager.GPS_PROVIDER))
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, 0, 0,
                        locationListener);
            Location location = locationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (location != null) {
                longitude = location.getLongitude();
            }
        }
        return longitude;
    }
}

Related Tutorials