我已经完成了这个tutorial。我遵循那里提到的每一步。我的纬度和经度分别为0.0和0.0。我想通过GPS阅读用户的当前位置。而且,我想为当前用户提供其他活动用户位置。我的项目基于通过GPS系统进行的公交跟踪。请参阅下面的代码。
Dynamic_Map.java:
public class Dynamic_Map extends
FragmentActivity { private GoogleMap googleMap; GPSTracker gps;
@Override protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamic_map);
try{
gps = new GPSTracker(this);
if(gps.canGetLocation()){
if(gps.isGPSEnabled){
double latitudee = gps.getLatitude();
double longitudee = gps.getLongitude();
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitudee + "\nLong: " + longitudee, Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Your Location is not available bcos gps is
"+gps.isGPSEnabled,Toast.LENGTH_LONG).show();
gps.showSettingsAlert();
}
}else{
gps.showSettingsAlert();
} initilizeMap(); } catch(Exception e) { e.printStackTrace(); } } }
GPSTracker.java
public class GPSTracker extends Service implements
LocationListener { private final Context mContext; private String
provider; boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false; Location location;// location
double latitude; double longitude; private static final
long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; private static final long
MIN_TIME_BW_UPDATES = 1000*60*1; protected LocationManager locationManager; public
GPSTracker(Context context){ this.mContext = context;
getLocation(); //onLocationChanged(location);
}
public Location getLocation(){ try{ LocationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, this);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnabled && !isNetworkEnabled){
}else{
this.canGetLocation = true;
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//isGPSEnabled = true;
//First get location from Network Provider
if(isNetworkEnabled){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
Log.d("Network","Network");
if(locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location !=null){
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
}
}
}}
}catch(Exception e){ e.printStackTrace(); }
return location; } @Override public void onLocationChanged(Location location){
latitude = location.getLatitude(); longitude = location.getLongitude(); } @Override public void
onProviderDisabled(String provider){ Toast.makeText(this, "Disabled
new provider " + provider,
Toast.LENGTH_SHORT).show(); } @Override
public void onProviderEnabled(String provider) { Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0){
return null;
}
/**
* Function to get latitude
*
*/ public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude, Toast.LENGTH_LONG).show();
}
//return latitude;
return latitude; }
/**
* Function to get longitude
*/ public double getLongitude(){
if(location !=null){
longitude = location.getLongitude();
Toast.makeText(getApplicationContext(), "Your Location is - \nLng: " + longitude, Toast.LENGTH_LONG).show();
}
//return longitude;
return longitude;
}
/**
* Function to check if best network provider
* @return boolean
*/ public boolean canGetLocation(){
return this.canGetLocation; }
/**
* Function to show settings alert dialog
*/ public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
//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?");
//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);
mContext.startActivity(intent);
}
});
//On pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which){
dialog.cancel();
}
});
//Showing Alert Message
alertDialog.show();
} /**
* Stop using GPS listener
* Calling this function will stop using GPS in app
*
*/
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
} } }
我在AndroidManifest.xml
文件中包含了必要的权限。需要显示当前位置。
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
uses-permission android:name="android.permission.INTERNET"/>
请帮帮我。