android定位源码 demo,LocationDemo(Android定位)

代码:

public class LocationUi extends BaseUi implements View.OnClickListener, LocationListener {

private Button mBtnStartLocation;

private Button mBtnStopLocation;

private TextView mTvLocation;

private LocationManager mLocationManager;

@Override

protected void initView() {

setContentView(R.layout.ui_location);

mBtnStartLocation = (Button) findViewById(R.id.location_btnStartLocation);

mBtnStopLocation = (Button) findViewById(R.id.location_btnStopLocation);

mTvLocation = (TextView) findViewById(R.id.location_tvLocation);

}

@Override

protected void initData() {

}

@Override

protected void initListener() {

mBtnStartLocation.setOnClickListener(this);

mBtnStopLocation.setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.location_btnStartLocation:

openGPSSettings();

break;

case R.id.location_btnStopLocation:

stopGPS();

break;

}

}

private void openGPSSettings() {

mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

if (mLocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {

Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();

doWork();

} else {

Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();

Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);

startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面

}

}

private void doWork() {

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

mTvLocation.setText("权限异常1");

return;

}

Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 8, this);

updata(location);

}

private void updata(Location location) {

if (location != null) {

String latitude = formatWGS84ToDegree(location.getLatitude());

String longitude = formatWGS84ToDegree(location.getLongitude());

StringBuilder sb = new StringBuilder();

sb.append("实时的位置信息:\n");

sb.append("经度:");

sb.append(longitude);

sb.append("\n纬度:");

sb.append(latitude);

sb.append("\b高度:");

sb.append(location.getAltitude());

sb.append("\n速度:");

sb.append(location.getSpeed());

sb.append("\n方向:");

sb.append(location.getBearing());

mTvLocation.setText(sb.toString());

}

}

@Override

public void onLocationChanged(Location location) {

// 当GPS定位信息发生改变时,更新位置

updata(location);

}

@Override

public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override

public void onProviderEnabled(String provider) {

// 当GPS Location Provider可用时,更新位置

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED

&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

mTvLocation.setText("权限异常2");

return;

}

updata(mLocationManager.getLastKnownLocation(provider));

}

@Override

public void onProviderDisabled(String provider) {

}

private void stopGPS() {

if (mLocationManager != null && mLocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {

mLocationManager.removeUpdates(this);

}

}

/**

* 将WGS84坐标由十进制转换为度分秒格式

*

* @param value

* @return 格式为114°12′20.12″

*/

public String formatWGS84ToDegree(double value) {

String result = "";

int degree, minute;

double second;

degree = (int) value;

minute = (int) ((value - degree) * 60);

second = ((value - degree) * 60 - minute) * 60;

// “秒”部分小数位数为三位

result = String.format(Locale.CHINA, "%s°%s′%.3f″", degree, minute, second);

return result;

}

}

xml:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/location_btnStartLocation"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="开启定位"

android:textAllCaps="false"/>

android:id="@+id/location_btnStopLocation"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="关闭定位"

android:textAllCaps="false"/>

android:id="@+id/location_tvLocation"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="10dp"

android:textColor="#000000"

android:textSize="20sp"/>

权限:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值