几个月前,我创建了GPSTracker库来帮助我获取GPS位置。如果您需要查看GPSTracker> getLocation
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
活动
import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.TextView;public class MainActivity extends Activity { TextView textview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.geo_locations); // check if GPS enabled GPSTracker gpsTracker = new GPSTracker(this); if (gpsTracker.getIsGPSTrackingEnabled()) { String stringLatitude = String.valueOf(gpsTracker.latitude); textview = (TextView)findViewById(R.id.fieldLatitude); textview.setText(stringLatitude); String stringLongitude = String.valueOf(gpsTracker.longitude); textview = (TextView)findViewById(R.id.fieldLongitude); textview.setText(stringLongitude); String country = gpsTracker.getCountryName(this); textview = (TextView)findViewById(R.id.fieldCountry); textview.setText(country); String city = gpsTracker.getLocality(this); textview = (TextView)findViewById(R.id.fieldCity); textview.setText(city); String postalCode = gpsTracker.getPostalCode(this); textview = (TextView)findViewById(R.id.fieldPostalCode); textview.setText(postalCode); String addressLine = gpsTracker.getAddressLine(this); textview = (TextView)findViewById(R.id.fieldAddressLine); textview.setText(addressLine); } else { // can't get location // GPS or Network is not enabled // Ask user to enable GPS/network in settings gpsTracker.showSettingsalert(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.varna_lab_geo_locations, menu); return true; }}GPS追踪器
import java.io.IOException;import java.util.List;import java.util.Locale;import android.app.alertDialog;import android.app.Service;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.location.Address;import android.location.Geoprer;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.os.IBinder;import android.provider.Settings;import android.util.Log;public class GPSTracker extends Service implements LocationListener { // Get Class Name private static String TAG = GPSTracker.class.getName(); private final Context mContext; // flag for GPS Status boolean isGPSEnabled = false; // flag for network status boolean isNetworkEnabled = false; // flag for GPS Tracking is enabled boolean isGPSTrackingEnabled = false; Location location; double latitude; double longitude; // How many Geoprer should return our GPSTracker int geoprerMaxResults = 1; // The minimum distance to change updates in meters private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters // The minimum time between updates in milliseconds private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute // Declaring a Location Manager protected LocationManager locationManager; // Store LocationManager.GPS_PROVIDER or LocationManager.NETWORK_PROVIDER information private String provider_info; public GPSTracker(Context context) { this.mContext = context; getLocation(); } public void getLocation() { try { locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); //getting GPS status isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); //getting network status isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); // Try to get location if you GPS Service is enabled if (isGPSEnabled) { this.isGPSTrackingEnabled = true; Log.d(TAG, "Application use GPS Service"); provider_info = LocationManager.GPS_PROVIDER; } else if (isNetworkEnabled) { // Try to get location if you Network Service is enabled this.isGPSTrackingEnabled = true; Log.d(TAG, "Application use Network State to get GPS coordinates"); provider_info = LocationManager.NETWORK_PROVIDER; } // Application can use GPS or Network Provider if (!provider_info.isEmpty()) { locationManager.requestLocationUpdates( provider_info, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this ); if (locationManager != null) { location = locationManager.getLastKnownLocation(provider_info); updateGPSCoordinates(); } } } catch (Exception e) { //e.printStackTrace(); Log.e(TAG, "Impossible to connect to LocationManager", e); } } public void updateGPSCoordinates() { if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } public double getLatitude() { if (location != null) { latitude = location.getLatitude(); } return latitude; } public double getLongitude() { if (location != null) { longitude = location.getLongitude(); } return longitude; } public boolean getIsGPSTrackingEnabled() { return this.isGPSTrackingEnabled; } public void stopUsingGPS() { if (locationManager != null) { locationManager.removeUpdates(GPSTracker.this); } } public void showSettingsalert() { alertDialog.Builder alertDialog = new alertDialog.Builder(mContext); //Setting Dialog Title alertDialog.setTitle(R.string.GPSalertDialogTitle); //Setting Dialog Message alertDialog.setMessage(R.string.GPSalertDialogMessage); //On Pressing Setting button alertDialog.setPositiveButton(R.string.action_settings, new DialogInterface.onClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); mContext.startActivity(intent); } }); //On pressing cancel button alertDialog.setNegativeButton(R.string.cancel, new DialogInterface.onClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); alertDialog.show(); } public List<Address> getGeoprerAddress(Context context) { if (location != null) { Geoprer geoprer = new Geoprer(context, Locale.ENGLISH); try { List<Address> addresses = geoprer.getFromLocation(latitude, longitude, this.geoprerMaxResults); return addresses; } catch (IOException e) { //e.printStackTrace(); Log.e(TAG, "Impossible to connect to Geoprer", e); } } return null; } public String getAddressLine(Context context) { List<Address> addresses = getGeoprerAddress(context); if (addresses != null && addresses.size() > 0) { Address address = addresses.get(0); String addressLine = address.getAddressLine(0); return addressLine; } else { return null; } } public String getLocality(Context context) { List<Address> addresses = getGeoprerAddress(context); if (addresses != null && addresses.size() > 0) { Address address = addresses.get(0); String locality = address.getLocality(); return locality; } else { return null; } } public String getPostalCode(Context context) { List<Address> addresses = getGeoprerAddress(context); if (addresses != null && addresses.size() > 0) { Address address = addresses.get(0); String postalCode = address.getPostalCode(); return postalCode; } else { return null; } } public String getCountryName(Context context) { List<Address> addresses = getGeoprerAddress(context); if (addresses != null && addresses.size() > 0) { Address address = addresses.get(0); String countryName = address.getCountryName(); return countryName; } else { return null; } } @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) { } @Override public IBinder onBind(Intent intent) { return null; }}注意
如果方法/答案不起作用。您需要使用官方的Google Provider: FusedLocationProviderApi 。



