GPSHelper.java文件:
GpsLocation.java
在这2个类里面获取的GPS坐标时,需要在手机上设置一个选项:允许手机使用无线网络通过基站来获取位置信息(这样可以保证在室内或室外都可以快速的获取GPS定位坐标)
需要加几个权限:
package edison.gps;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class GPSHelper {
private Context con;
private double mLatitude = 0;
private double mLongitude = 0;
private LocationManager mLocationManager;
public GPSHelper(Context con) {
this.con = con;
}
public static GPSHelper mGps;
public static GPSHelper getGpsInstance(Context mCon) {
if (mGps == null) {
mGps = new GPSHelper(mCon);
}
return mGps;
}
/** Get the locationManager object and set GPS update listener. */
public void openGps() {
mLocationManager = (LocationManager) con
.getSystemService(Context.LOCATION_SERVICE);
/**
* Register the listener with the Location Manager to receive
* locationupdates
*/
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 6000, 0, locationListener);
}
/** Close the gps service. */
public void closeGps() {
if (mLocationManager != null) {
mLocationManager.removeUpdates(locationListener);
}
}
/** Get the latitude location. */
public double getLatitude() {
return mLatitude;
}
/** Get the longitude location. */
public double getLongitude() {
return mLongitude;
}
private void updateWithNewLocation(Location location) {
if (location != null) {
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
}
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
}
GpsLocation.java
package edison.gps;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class GpsLocation extends Activity {
private Button mGetLocation;
private TextView mLongitude, mLatitude;
private GPSHelper mGps;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGps = GPSHelper.getGpsInstance(getApplicationContext());
findView();
}
public void findView() {
mGetLocation = (Button) findViewById(R.id.mGetLocation);
mGetLocation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mGps.openGps();
mLongitude.setText(String.valueOf(mGps.getLongitude()));
mLatitude.setText(String.valueOf(mGps.getLatitude()));
}
});
mLongitude = (TextView) findViewById(R.id.mLongitude);
mLatitude = (TextView) findViewById(R.id.mLatitude);
}
}
在这2个类里面获取的GPS坐标时,需要在手机上设置一个选项:允许手机使用无线网络通过基站来获取位置信息(这样可以保证在室内或室外都可以快速的获取GPS定位坐标)
需要加几个权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />