百度地图api实现service后台定位

倚窗听风2013 2013-03-10 02:00:15
话说菜鸟做的一个有关定位的应用,开始用的googleapi,做成后感觉效果不佳。遂最近改用百度地图api。但在实现后台定位(在service中定位)时多次尝试失败,在定位时返回错误代码,说服务没有开启。网上也没有找到这方面的资料,希望大神们有类似经验的提示一下,最好有个样例代码。下面附上我的这部分代码。

package com.atd_1.activity;

import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.LocationData;

import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Criteria;
import android.location.GpsStatus.Listener;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;

public class GPSService extends Service
{
private MyGPSBinder mbinder;
public LocationClient mLocationClient = null;
public BDLocationListener myListener = new MyLocationListener2();
public LocationData locData = new LocationData();

@Override
public void onCreate()
{
super.onCreate();
mbinder = new MyGPSBinder();
mLocationClient = new LocationClient(this); // 声明LocationClient类
mLocationClient.registerLocationListener(myListener); // 注册监听函数
mLocationClient = new LocationClient(this);
}

@Override
public IBinder onBind(Intent intent)
{
return mbinder;
}

@Override
public void onDestroy()
{
System.out.println("停止GPS后台服务!");
if (mLocationClient.isStarted())
{
// mLocationClient.stop();
System.out.println("关闭SDK服务");
}
stopSelf();
super.onDestroy();
}

class MyGPSBinder extends Binder
{
/**
* 开启GPS服务
*/
public void start()
{
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
option.setAddrType("detail");
option.setCoorType("gcj02");
option.setScanSpan(5000);
option.disableCache(true);// 禁止启用缓存定位
mLocationClient.setLocOption(option);
if (!mLocationClient.isStarted())
{
mLocationClient.start();
System.out.println("开启定位SDK服务");
if (!mLocationClient.isStarted())
{
mLocationClient.start();
System.out.println("开启定位SDK服务2");
}
}
return;
}

/**
* 停止GPS服务
*/
public void stop()
{
}

/**
* 获取GPS位置对象
*/
public LocationData getLocation()
{
start();
System.out.println("获取位置" + mLocationClient.requestLocation());
if (mLocationClient != null && mLocationClient.isStarted())
{
System.out.println("获取位置" + mLocationClient.requestLocation());
return locData;
} else if (!mLocationClient.isStarted())
{
System.out.printf("locClient is not started");
return null;
} else
{
System.out.println("locClient is null");
return null;
}
}

}

}
//////////////////////////////////////////////////////////////
package com.atd_1.activity;

import java.util.HashMap;

import android.annotation.SuppressLint;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;

public class MyLocationListener2 implements BDLocationListener
{

// HashMap<String, String> locationInformation = new HashMap<String, String>();
// HashMap<String, String> poiInformation = new HashMap<String, String>();

@Override
public void onReceiveLocation(BDLocation location)
{
System.out.println("错误代码:---------------"+location.getLocType());
System.out.println("函数被调用");
if (location == null)
{
System.out.println("location返回为空值!");
return;
}
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
sb.append("\nlatitude : ");
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation)
{
sb.append("\nspeed : ");
sb.append(location.getSpeed());
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation)
{
sb.append("\naddr : ");
sb.append(location.getAddrStr());
}

System.out.println("11111" + sb.toString());
}

public void onReceivePoi(BDLocation poiLocation)
{
if (poiLocation == null)
{
return;
}
StringBuffer sb = new StringBuffer(256);
sb.append("Poi time : ");
sb.append(poiLocation.getTime());
sb.append("\nerror code : ");
sb.append(poiLocation.getLocType());
sb.append("\nlatitude : ");
sb.append(poiLocation.getLatitude());
sb.append("\nlontitude : ");
sb.append(poiLocation.getLongitude());
sb.append("\nradius : ");
sb.append(poiLocation.getRadius());
if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation)
{
sb.append("\naddr : ");
sb.append(poiLocation.getAddrStr());
}
if (poiLocation.hasPoi())
{
sb.append("\nPoi:");
sb.append(poiLocation.getPoi());
} else
{
sb.append("noPoi information");
}
// logMsg(sb.toString());
}
}
...全文
2485 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
max_nee 2014-11-11
  • 打赏
  • 举报
回复
用service可以调用定位。 我在百度提供的demo中增加一个Service,然后将定位的先相关代码都放在Service中,Activity中只做了一个启动Service的操作,可以成功定位。
sharpbrind 2014-09-15
  • 打赏
  • 举报
回复
楼主解决了吗?我也遇到同样问题。 从官网文档上看,启动必须在主线程中才行,我的程序没有UI就不行,用UI又违背需求。 QQ:312223975 看到后可以一起探讨。
Kdo 2013-07-11
  • 打赏
  • 举报
回复
试试把定位方法放在service的onstart方法里
灰羽_GrayPlumage 2013-07-05
  • 打赏
  • 举报
回复
(1)mlocationClient.start();后面加上mlocationClient.requestLocation();发起定位。 (2)AndroidManifest.xml里要写上定位相关权限,如 <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" > </uses-permission> 等等,还要写上service,具体看百度地图API的说明,或者直接复制Demo里的相关以免遗漏。 (3)电脑上的Android虚拟机一般没法定位,可以连上手机打开GPS试试,不过按经验也得多定几次才能成功,毕竟稳定性不足。
a3761635 2013-05-10
  • 打赏
  • 举报
回复
在模拟器中很难得到gps、网络定位结果。而且在室内,手机上采用gps定位也很可能得不到结果。可以试试在手机上采用网络定位。LocationClientOption.setPriority(LocationClientOption.NetWorkFirst)。
沸腾 2013-03-18
  • 打赏
  • 举报
回复
楼主问题解决了吗?最近也在做类似尝试啊。 本来是直接GPS定位的,发现GPS定位数据居然和百度的定位数据不一样,无耐只能改百度定位,不知道百度定位在Service中能不能实现啊。
南瓜饼 2013-03-18
  • 打赏
  • 举报
回复
楼主报的什么错误提示啊 再xml里面加权限了嘛?

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧