在真机上 模拟GPS
2014-11-11 09:07
127 查看
必须先开启“允许模拟地点”
方法: 打开设备的,设置 - 应用程序 - 开发 - “允许模拟地点”
1.在 AndroidManifest.xml 里添加相应的 “允许模拟位置的权限”
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
2.创建 一个 “用于模拟的坐标提供者”
LocationManager locationManager = (LocationManager) mContext
.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.getProvider(mMockProviderName) == null) {
locationManager.addTestProvider(mMockProviderName, false, true,
false, false, false, false, false, 0, 5);
locationManager.setTestProviderEnabled(mMockProviderName, true);
}
注意:mMockProviderName 是你随意定的名称,后面在使用(获得位置)时,用这个名称。
3.构建(模拟的)坐标位置
Location loc = new Location(mMockProviderName);
loc.setTime(System.currentTimeMillis());
loc.setLatitude(latitude);
loc.setLongitude(longitude);
locationManager.setTestProviderLocation(mMockProviderName, loc);
Log.i("gps", String.format("once: x=%s y=%s", longitude, latitude));
注意:我们可以多次间歇的调用此方法来 模拟 位置的变化。
4.获得(使用)坐标位置。
loc = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
loc.requestLocationUpdates(NAME_MOCK_GPS_PROVIDER, 1000, 10,
mListener);
这里的NAME_MOCK_GPS_PROVIDER的名称 和前面“第2步”里名称是一致的。
好了。让代码跑起来。
------------------------------------
package pdwy.qyzyMobile.GisModule;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Handler;
import android.util.Log;
class GpsMockManager {
private Context mContext;
Handler mHandler1;
public String mMockProviderName = "spoof";
private int mPostDelayed = 10000;
GpsFakePointProvider mGpsFakePointProvider;
public GpsMockManager(String mockProviderName, Context context,
Handler handler) {
super();
mMockProviderName = mockProviderName;
mContext = context;
mHandler1 = handler;
// mGpsFakePointProvider = new ChangedPointProvider();
mGpsFakePointProvider = new StaticPointProvider();
}
public String getmMockProviderName() {
return mMockProviderName;
}
Runnable fakeProc = new Runnable() {
@Override
public void run() {
GpsFakePoint pnt = mGpsFakePointProvider.getGpsFakePoint();
setLocation(pnt.longitude, pnt.latitude);
mHandler1.postDelayed(fakeProc, mPostDelayed);
}
};
public void RunFake() {
mHandler1.postDelayed(fakeProc, mPostDelayed);
}
public void StopFake() {
mHandler1.removeCallbacks(fakeProc);
}
private void setLocation(double longitude, double latitude) {
LocationManager locationManager = (LocationManager) mContext
.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.getProvider(mMockProviderName) == null) {
locationManager.addTestProvider(mMockProviderName, false, true,
false, false, false, false, false, 0, 5);
locationManager.setTestProviderEnabled(mMockProviderName, true);
}
Location loc = new Location(mMockProviderName);
loc.setTime(System.currentTimeMillis());
loc.setLatitude(latitude);
loc.setLongitude(longitude);
locationManager.setTestProviderLocation(mMockProviderName, loc);
Log.i("gps", String.format("once: x=%s y=%s", longitude, latitude));
}
public class GpsFakePoint {
public GpsFakePoint(double longitude, double latitude) {
super();
this.longitude = longitude;
this.latitude = latitude;
}
public double latitude;
public double longitude;
}
public interface GpsFakePointProvider {
public GpsFakePoint getGpsFakePoint();
}
public class ChangedPointProvider implements GpsFakePointProvider {
private double mLatitude = 1;
private double mLongitude = 1;
@Override
public GpsFakePoint getGpsFakePoint() {
return new GpsFakePoint(mLatitude++, mLongitude++);
}
}
public class StaticPointProvider implements GpsFakePointProvider {
public GpsFakePoint getGpsFakePoint() {
return new GpsFakePoint(116.9152, 39.4435);
//return new GpsFakePoint(116.8737, 39.4328);//偏左
//return new GpsFakePoint(116.886,39.4257);
}
}
}// end class GpsMockManager
方法: 打开设备的,设置 - 应用程序 - 开发 - “允许模拟地点”
1.在 AndroidManifest.xml 里添加相应的 “允许模拟位置的权限”
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
2.创建 一个 “用于模拟的坐标提供者”
LocationManager locationManager = (LocationManager) mContext
.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.getProvider(mMockProviderName) == null) {
locationManager.addTestProvider(mMockProviderName, false, true,
false, false, false, false, false, 0, 5);
locationManager.setTestProviderEnabled(mMockProviderName, true);
}
注意:mMockProviderName 是你随意定的名称,后面在使用(获得位置)时,用这个名称。
3.构建(模拟的)坐标位置
Location loc = new Location(mMockProviderName);
loc.setTime(System.currentTimeMillis());
loc.setLatitude(latitude);
loc.setLongitude(longitude);
locationManager.setTestProviderLocation(mMockProviderName, loc);
Log.i("gps", String.format("once: x=%s y=%s", longitude, latitude));
注意:我们可以多次间歇的调用此方法来 模拟 位置的变化。
4.获得(使用)坐标位置。
loc = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
loc.requestLocationUpdates(NAME_MOCK_GPS_PROVIDER, 1000, 10,
mListener);
这里的NAME_MOCK_GPS_PROVIDER的名称 和前面“第2步”里名称是一致的。
好了。让代码跑起来。
------------------------------------
package pdwy.qyzyMobile.GisModule;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Handler;
import android.util.Log;
class GpsMockManager {
private Context mContext;
Handler mHandler1;
public String mMockProviderName = "spoof";
private int mPostDelayed = 10000;
GpsFakePointProvider mGpsFakePointProvider;
public GpsMockManager(String mockProviderName, Context context,
Handler handler) {
super();
mMockProviderName = mockProviderName;
mContext = context;
mHandler1 = handler;
// mGpsFakePointProvider = new ChangedPointProvider();
mGpsFakePointProvider = new StaticPointProvider();
}
public String getmMockProviderName() {
return mMockProviderName;
}
Runnable fakeProc = new Runnable() {
@Override
public void run() {
GpsFakePoint pnt = mGpsFakePointProvider.getGpsFakePoint();
setLocation(pnt.longitude, pnt.latitude);
mHandler1.postDelayed(fakeProc, mPostDelayed);
}
};
public void RunFake() {
mHandler1.postDelayed(fakeProc, mPostDelayed);
}
public void StopFake() {
mHandler1.removeCallbacks(fakeProc);
}
private void setLocation(double longitude, double latitude) {
LocationManager locationManager = (LocationManager) mContext
.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.getProvider(mMockProviderName) == null) {
locationManager.addTestProvider(mMockProviderName, false, true,
false, false, false, false, false, 0, 5);
locationManager.setTestProviderEnabled(mMockProviderName, true);
}
Location loc = new Location(mMockProviderName);
loc.setTime(System.currentTimeMillis());
loc.setLatitude(latitude);
loc.setLongitude(longitude);
locationManager.setTestProviderLocation(mMockProviderName, loc);
Log.i("gps", String.format("once: x=%s y=%s", longitude, latitude));
}
public class GpsFakePoint {
public GpsFakePoint(double longitude, double latitude) {
super();
this.longitude = longitude;
this.latitude = latitude;
}
public double latitude;
public double longitude;
}
public interface GpsFakePointProvider {
public GpsFakePoint getGpsFakePoint();
}
public class ChangedPointProvider implements GpsFakePointProvider {
private double mLatitude = 1;
private double mLongitude = 1;
@Override
public GpsFakePoint getGpsFakePoint() {
return new GpsFakePoint(mLatitude++, mLongitude++);
}
}
public class StaticPointProvider implements GpsFakePointProvider {
public GpsFakePoint getGpsFakePoint() {
return new GpsFakePoint(116.9152, 39.4435);
//return new GpsFakePoint(116.8737, 39.4328);//偏左
//return new GpsFakePoint(116.886,39.4257);
}
}
}// end class GpsMockManager
相关文章推荐
- arcgis for android 学习 - (7) 在真机上 模拟GPS
- 真机上模拟GPS
- arcgis for android 学习 - (7) 在真机上 模拟GPS
- arcgis for android 学习 - (7) 在真机上 模拟GPS
- 使用Socket通信实现Silverlight客户端实时数据的获取(模拟GPS数据,地图实时位置)
- 二级菜单在android模拟中无法使用,在真机上可以使用
- AndroidStudio实现真机模拟以及一些问题解决
- Android 【真机】与【模拟器】触摸屏事件的模拟差异分析
- Android初学者首次真机模拟adb出现问题解决方法:
- Android 【真机】与【模拟器】触摸屏事件的模拟差异分析
- android模拟器模拟定位GPS
- IOS模拟器中GPS数据的模拟
- iOS开发 弱网环境模拟(真机)
- iOS 真机模拟内存警告
- Android 模拟手机GPS入门
- M2真机模拟事件
- 使用Socket通信实现Silverlight客户端实时数据的获取(模拟GPS数据,地图实时位置)
- GPS实时跟踪程序模拟(2)动画回放 推荐
- Android 【真机】与【模拟器】触摸屏事件的模拟差异分析
- 使用Socket通信实现Silverlight客户端实时数据的获取(模拟GPS数据,地图实时位置)