use of com.jamesfchen.map.ILbsListener in project spacecraft-android by JamesfChen.
the class LbsServices method realRequestLocationForAmap.
@SuppressLint("MissingPermission")
private void realRequestLocationForAmap() {
mlocationClient.setLocationListener(new AMapLocationListener() {
// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
// 注意设置合适的定位时间的间隔(最小间隔支持为1000ms),并且在合适时间调用stopLocation()方法来取消定位请求
// 在定位结束后,在合适的生命周期调用onDestroy()方法
// 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
@Override
public void onLocationChanged(AMapLocation amapLocation) {
if (amapLocation != null) {
if (amapLocation.getErrorCode() == 0) {
// 定位成功回调信息,设置相关消息
// 获取当前定位结果来源,如网络定位结果,详见定位类型表
int locationType = amapLocation.getLocationType();
// 获取纬度
double latitude = amapLocation.getLatitude();
// 获取经度
double longitude = amapLocation.getLongitude();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(amapLocation.getTime());
// 定位时间
df.format(date);
String locationTypeStr;
switch(locationType) {
// case 0:{locationTypeStr = "定位失败";}
case 1:
{
locationTypeStr = "GPS定位结果";
}
case 2:
{
locationTypeStr = "前次定位结果";
}
// case 3:{locationTypeStr = "缓存定位结果";}
case 4:
{
locationTypeStr = "缓存定位结果";
}
case 5:
{
locationTypeStr = "Wifi定位结果";
}
case 6:
{
locationTypeStr = "基站定位结果";
}
// case 7:{locationTypeStr = "离线定位结果";}
case 8:
{
locationTypeStr = "离线定位结果";
}
case 9:
{
locationTypeStr = "最后位置缓存";
}
default:
{
locationTypeStr = "";
}
}
Log.d(TAG_service, "IntentService onLocationChanged: locationType:" + locationTypeStr + " location" + latitude + " , " + longitude + "");
if (iLbsApi.listenerlist == null)
return;
final int N = iLbsApi.listenerlist.beginBroadcast();
for (int i = 0; i < N; i++) {
ILbsListener l = iLbsApi.listenerlist.getBroadcastItem(i);
if (l != null) {
try {
List<AppCellInfo> appCellInfos = new ArrayList<>();
for (CellInfo cell : telephonyManager.getAllCellInfo()) {
appCellInfos.add(AppCellInfo.convertSysCellInfo(cell));
}
count += 1;
l.onLocationChanged(AppLocation.convertSysLocation(amapLocation), appCellInfos, count);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
iLbsApi.listenerlist.finishBroadcast();
} else {
// 显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
Log.e(TAG_service, "location Error, ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo());
}
}
}
});
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
// 设置定位间隔,单位毫秒,默认为2000ms
mLocationOption.setInterval(2000);
mlocationClient.setLocationOption(mLocationOption);
mlocationClient.startLocation();
}
use of com.jamesfchen.map.ILbsListener in project spacecraft-android by JamesfChen.
the class ILbsApiServer method mockRequestLocation.
private void mockRequestLocation() {
if (listenerlist == null)
return;
Type type = new TypeToken<List<L7>>() {
}.getType();
List<L7> l7_list = new Gson().fromJson(ResourceUtils.readAssets2String("l7_list.json"), type);
try {
Thread.sleep(1500);
for (L7 l7 : l7_list) {
double lat = l7.appLocation.lat;
double lon = l7.appLocation.lon;
long cid = l7.appCellInfos.get(0).get(0).cid;
long lac = l7.appCellInfos.get(0).get(0).lac;
// startservice 回调(onHandleIntent) 和bindservice回调(onServiceConnected) 都是异步,所以sleep模拟onHandleIntent处理耗时,
// Thread.sleep(500);
final int N = listenerlist.beginBroadcast();
for (int i = 0; i < N; i++) {
ILbsListener l = listenerlist.getBroadcastItem(i);
if (l == null)
continue;
AppCellInfo appCellInfo = new AppCellInfo();
appCellInfo.cid = cid;
appCellInfo.lac = lac;
appCellInfo.isRegistered = true;
appCellInfo.isMockData = true;
AppLocation appLocation = new AppLocation();
appLocation.lat = lat;
appLocation.lon = lon;
appLocation.isMockData = true;
++count;
l.onLocationChanged(appLocation, Collections.singletonList(appCellInfo), count);
}
listenerlist.finishBroadcast();
}
} catch (InterruptedException | RemoteException e) {
e.printStackTrace();
}
}
use of com.jamesfchen.map.ILbsListener in project spacecraft-android by JamesfChen.
the class LbsServices method realRequestLocation.
@SuppressLint("MissingPermission")
private void realRequestLocation() {
List<String> enableProviders = locationManager.getProviders(true);
List<String> allProviders = locationManager.getAllProviders();
Log.d("LBS_collection", "onHandleIntent: enable provider:" + enableProviders.toString());
Log.d("LBS_collection", "onHandleIntent: all provider:" + allProviders);
String choice;
boolean gpsProviderEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (gpsProviderEnabled) {
choice = LocationManager.GPS_PROVIDER;
} else {
boolean netProviderEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (netProviderEnabled) {
choice = LocationManager.NETWORK_PROVIDER;
} else {
choice = LocationManager.PASSIVE_PROVIDER;
}
}
Log.d("LBS_collection", "onHandleIntent: choice provider:" + choice);
locationManager.requestLocationUpdates(choice, MIN_TIMES, MIN_DISTANCE, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.d(TAG_service, "IntentService onLocationChanged: location" + location.getLatitude() + " , " + location.getLongitude());
if (iLbsApi.listenerlist == null)
return;
final int N = iLbsApi.listenerlist.beginBroadcast();
for (int i = 0; i < N; i++) {
ILbsListener l = iLbsApi.listenerlist.getBroadcastItem(i);
if (l != null) {
try {
List<AppCellInfo> appCellInfos = new ArrayList<>();
for (CellInfo cell : telephonyManager.getAllCellInfo()) {
appCellInfos.add(AppCellInfo.convertSysCellInfo(cell));
}
count += 1;
l.onLocationChanged(AppLocation.convertSysLocation(location), appCellInfos, count);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
iLbsApi.listenerlist.finishBroadcast();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
if (iLbsApi.listenerlist == null)
return;
final int N = iLbsApi.listenerlist.beginBroadcast();
for (int index = 0; index < N; i++) {
ILbsListener l = iLbsApi.listenerlist.getBroadcastItem(index);
if (l != null) {
try {
l.onStatusChanged(s, i, bundle);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
iLbsApi.listenerlist.finishBroadcast();
}
@Override
public void onProviderEnabled(String s) {
if (iLbsApi.listenerlist == null)
return;
final int N = iLbsApi.listenerlist.beginBroadcast();
for (int i = 0; i < N; i++) {
ILbsListener l = iLbsApi.listenerlist.getBroadcastItem(i);
if (l != null) {
try {
l.onProviderEnabled(s);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
iLbsApi.listenerlist.finishBroadcast();
}
@Override
public void onProviderDisabled(String s) {
if (iLbsApi.listenerlist == null)
return;
final int N = iLbsApi.listenerlist.beginBroadcast();
for (int i = 0; i < N; i++) {
ILbsListener l = iLbsApi.listenerlist.getBroadcastItem(i);
if (l != null) {
try {
l.onProviderDisabled(s);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
iLbsApi.listenerlist.finishBroadcast();
}
}, Looper.getMainLooper());
}
Aggregations