使用GPS获取经纬度

GPSHelper.java文件:

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" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值