是否有一种无需显示地图即可获取信息的方法?我想在工作中与其他活动取得位置上的协调。tnx很多。
使用以下代码。它对我有用。
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
public class GPSTracker extends Service implements LocationListener{
private Context context;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude,longitude;
LocationManager locationManager;
AlertDialogManager am = new AlertDialogManager();
public GPSTracker(Context context){
this.context = context;
getLocation();
}
private Location getLocation() {
// TODO Auto-generated method stub
try{
locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled){
} else {
this.canGetLocation = true;
if (isNetworkEnabled){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 3, this);
if (locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled){
if (location == null){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 3, this);
if (locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
} else {
showAlertDialog();
}
}
}catch(Exception e){
e.printStackTrace();
}
return location;
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(GPSTracker.this);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// Setting Icon to Dialog
//alertDialog.setIcon(R.drawable.delete);
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
dialog.cancel();
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
public void showAlertDialog(){
am.showAlertDialog(GPSTracker.this, "GPS Setting", "Gps is not enabled. Do you want to enabled it ?", false);
}
public double getLatitude(){
if (location != null){
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude(){
if (location != null){
longitude = location.getLongitude();
}
return longitude;
}
public boolean canGetLocation(){
return this.canGetLocation;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null){
this.location = location;
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
并且在如下的MainActivity
调用函数中。
GPSTracker gps = new GPSTracker (this);
double latitude = gps.getLatitude();
double longitude= gps.getLongitude();
通过使用LocationManager
,您可以获取lat和lang:
LocationManager
对LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
的调用不会被阻止-这意味着如果当前没有位置可用,它将返回getLastKnownLocation()
-因此,您可能想看看将null
传递给LocationListener
,因此将为您提供位置的异步更新。
LocationListener
如果您想使用GPS,则需要给您的应用程序requestLocationUpdates()
method。
requestLocationUpdates()
您可能还想在GPS不可用时添加private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
}
lm.requestLocationUpdates(LocationManager.GPS, 2000, 10, locationListener);
,并用ACCESS_FINE_LOCATION
permission选择您的位置提供者。
使用此代码能够获取纬度和经度值,并使确保在使用应用程序之前,您已获得清单的访问权限
ACCESS_FINE_LOCATION
如果您想在没有互联网的情况下使用此方法,请使用此选项>>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />