In this blog explain how to get location via GPS and Satellite in android.
First you can add some permission in manifest file.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
activity_main.xml.................................................................................<?xmlversion="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"><TextViewandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#066001" android:textSize="20dp" android:textStyle="bold" android:text="ANDROID BEGINNER POINT " /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Get Location" android:id="@+id/btnGPSShowLocation" android:textStyle="bold" android:layout_margin="10dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text=" " android:textStyle="bold" android:layout_margin="10dp" android:textColor="#e04545" android:id="@+id/textlati" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " android:textColor="#1d30c4" android:textStyle="bold" android:layout_margin="10dp" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textlongi" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:layout_margin="10dp" android:textColor="#d324d6" android:textStyle="bold" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textalti" /> </LinearLayout>...............................................................................
LocationService.java...............................................................................
public class LocationService extends Service implements LocationListener { boolean isGPSEnabled = false; // flag for GPS satellite status
boolean isNetworkEnabled = false; // flag for cellular network status
boolean canGetLocation = false; // flag for either cellular or satellite status
private final Context mContext; protected LocationManager locationManager; protected GpsListener gpsListener = new GpsListener(); Location location; // location
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 0 meters
private static final long MIN_TIME_BW_UPDATES = 1000; //1 second
public LocationService(Context context) { this.mContext = context; getLocation(); } class GpsListener implements GpsStatus.Listener { @Override
public void onGpsStatusChanged(int event) { } } public Location getLocation() { try { locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); // getting GPS satellite status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); // getting cellular network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { showSettingsAlert("GPS is not enabled! Want to go to settings menu?"); } else { this.canGetLocation = true; if (isNetworkEnabled) {//GPS is enabled, getting lat/long via cellular towers locationManager.addGpsStatusListener(gpsListener);//inserted new locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("Cell tower", "Cell tower"); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } } if (isGPSEnabled) {//GPS is enabled, gettoing lat/long via satellite if (location == null) { locationManager.addGpsStatusListener(gpsListener);//inserted new locationManager.getGpsStatus(null); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("GPS Enabled", "GPS Enabled"); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); } } } } } catch (Exception e) { e.printStackTrace(); } return location; } @Override
public void onLocationChanged(Location location) { } @Override
public void onStatusChanged(String provider, int status, Bundle extras) {
} @Override
public void onProviderEnabled(String provider) { } @Override
public void onProviderDisabled(String provider) { } @Nullable @Override
public IBinder onBind(Intent intent) { return null; } public void showSettingsAlert(String provider) { AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); alertDialog.setTitle(provider + "SETTINGS"); alertDialog.setMessage(provider); alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); mContext.startActivity(intent); } }); alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); alertDialog.show(); } }
......................................................................................................................................................
MainActivity.java
......................................................................................
public class MainActivity extends Activity { Button btnGPSShowLocation; LocationService appLocationService; TextView textlati, textlongi, textalti; Location gpsLocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); appLocationService = new LocationService(this); textlati = (TextView) findViewById(R.id.textlati); textlongi = (TextView) findViewById(R.id.textlongi); textalti = (TextView) findViewById(R.id.textalti); btnGPSShowLocation = (Button) findViewById(R.id.btnGPSShowLocation); btnGPSShowLocation.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { gpsLocation=appLocationService.getLocation(); if (gpsLocation != null) { double latitude = gpsLocation.getLatitude(); double longitude = gpsLocation.getLongitude(); double altitude = gpsLocation.getAltitude(); textlati.setText("Latitude : " + latitude + " "); textlongi.setText("Longitude : " + longitude + " "); textalti.setText("Altitude : " + altitude + " "); } } }); } }
....................................................................................
Please share Source Code link of this Demo...
ReplyDelete