android 定位服务

package com.epapnavigation.service;


import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;


public class Gps {
private Location location = null;
private LocationManager locationManager = null;
private Context context = null;


/**
* 初始化

* @param context
*/
public Gps(Context ctx) {
context = ctx;
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(getProvider());
//GPS定位
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 1, locationListener);
//网格定位
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
1000, 1, locationListener);
}


// 获取Location Provider
private String getProvider() {
// 构建位置查询条件
Criteria criteria = new Criteria();
// 查询精度:高
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
// 是否查询海拨:否
criteria.setAltitudeRequired(false);
// 是否查询方位角 : 否
criteria.setBearingRequired(false);
// 是否允许付费:是
criteria.setCostAllowed(false);
// 电量要求:低
criteria.setPowerRequirement(Criteria.POWER_LOW);
// 返回最合适的符合条件的provider,第2个参数为true说明 , 如果只有一个provider是有效的,则返回当前provider
return locationManager.getBestProvider(criteria, true);
}


private LocationListener locationListener = new LocationListener() {
// 位置发生改变后调用
public void onLocationChanged(Location l) {
if (l != null) {
location = l;
}
}


// provider 被用户关闭后调用
public void onProviderDisabled(String provider) {
location = null;
}


// provider 被用户开启后调用
public void onProviderEnabled(String provider) {
Location l = locationManager.getLastKnownLocation(provider);
if (l != null) {
location = l;
}


}


// provider 状态变化时调用
public void onStatusChanged(String provider, int status, Bundle extras) {
}


};


//get Location
public Location getLocation() {
return location;
}


public void closeLocation() {
if (locationManager != null) {
if (locationListener != null) {
locationManager.removeUpdates(locationListener);
locationListener = null;
}
locationManager = null;
}
}


}


package com.epapnavigation.service;


import java.math.BigDecimal;


import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.IBinder;


public class GpsService extends Service {

private Gps gps = null;
private boolean threadDisable = false;
Location location = null;
Double dbLatitude = 0.0;
Double dbLongitude=0.0;



@Override
public void onCreate() {
super.onCreate();
gps = new Gps(GpsService.this);
new Thread(new Runnable() {
@Override
public void run() {
while (!threadDisable) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (gps != null) { // 当结束服务时gps为空
// 获取经纬度
location = gps.getLocation();
// gps无法获取经纬度
if (location == null) {
System.out.println("----------->gps location null");
// 发送广播
Intent intent = new Intent();
intent.putExtra("lat",0.0);
intent.putExtra("lon",0.0);
intent.setAction("com.epapandroid.services.GpsService");
sendBroadcast(intent);
}
else {
// 发送广播
Intent intent = new Intent();
//BigDecimal bdla = new BigDecimal(location.getLatitude());
//dbLatitude = bdla.setScale(7,BigDecimal.ROUND_HALF_UP).doubleValue();  
//BigDecimal bdlo = new BigDecimal(location.getLongitude());
//dbLongitude = bdlo.setScale(7,BigDecimal.ROUND_HALF_UP).doubleValue();  
dbLatitude = location.getLatitude();
dbLongitude = location.getLongitude();
intent.putExtra("lat",
dbLatitude == 0.0 ? 0.0 : dbLatitude);
intent.putExtra("lon",
dbLongitude == 0.0 ? 0.0 : dbLongitude);
intent.setAction("com.epapnavigation.service.GpsService");
System.out.println("GpsService----->"+dbLatitude);
sendBroadcast(intent);
}
}


}
}
}).start();


}


@Override
public void onDestroy() {
threadDisable = true;

if (gps != null) {
gps.closeLocation();
gps = null;
}
super.onDestroy();
}


@Override
public IBinder onBind(Intent arg0) {
return null;
}


}


说明:定位经纬度在百度地图上相差两条街的距离,用百度地图,还是用百度地图定位来做。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值