weex☞获取经纬度
2018-12-07 本文已影响48人
小学生的博客
获取通讯录、扫码、打开相册、拍照等操作,都是一个套路,就是创建module,原生实现这些方法,前端调用是时候返回最终的结果。
下面是如何获取经纬度坐标。(原生获取部分参考网上代码,有价值的只是返回数据)
public class GetLocation extends WXModule {
protected JSCallback callback;
private LocationManager locationManager;
private String locationProvider;
public final static int LOCAL_REQUEST_CODE = 0;
@JSMethod(uiThread = true)
public void getLocal(JSCallback callback) {
this.callback = callback;
locationManager = (LocationManager) mWXSDKInstance.getContext().getSystemService(mWXSDKInstance.getContext().LOCATION_SERVICE);
//下面注释的代码获取的location为null,所以采用Criteria的方式。
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);//低精度,如果设置为高精度,依然获取不了location。
criteria.setAltitudeRequired(false);//不要求海拔
criteria.setBearingRequired(false);//不要求方位
criteria.setCostAllowed(true);//允许有花费
criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗
//从可用的位置提供器中,匹配以上标准的最佳提供器
locationProvider = locationManager.getBestProvider(criteria, true);
if (ActivityCompat.checkSelfPermission(mWXSDKInstance.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mWXSDKInstance.getContext(),
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.d("tag-local", " 没有权限 就获取权限");
ActivityCompat.requestPermissions((WXPageActivity) mWXSDKInstance.getContext(),
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
LOCAL_REQUEST_CODE);
}
Location location = locationManager.getLastKnownLocation(locationProvider);
if (location != null) {
//不为空,显示地理位置经纬度
showLocation(callback, location);
}
//监视地理位置变化
locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);
}
@JSMethod(uiThread = false)
public void removeLocalListener(JSCallback callback) {
locationManager.removeUpdates(locationListener);
callback.invoke("success");
}
/**
* LocationListern监听器
* 参数:地理位置提供器、监听位置变化的时间间隔、位置变化的距离间隔、LocationListener监听器
*/
LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle arg2) {
}
@Override
public void onProviderEnabled(String provider) {
Log.d("tag-local", "onProviderEnabled: " + provider + ".." + Thread.currentThread().getName());
}
@Override
public void onProviderDisabled(String provider) {
Log.d("tag-local", "onProviderDisabled: " + provider + ".." + Thread.currentThread().getName());
}
@Override
public void onLocationChanged(Location location) {
Log.d("tag-local", "onLocationChanged: " + ".." + Thread.currentThread().getName());
//如果位置发生变化,重新显示
showLocation(callback, location);
}
};
private void showLocation(JSCallback callback, Location location) {
Log.d("tag-local", "定位成功------>经度为:" + location.getLatitude() + "\n纬度为" + location.getLongitude());
Map data2 = new HashMap();
data2.put("result", true);
data2.put("Latitude", location.getLatitude());
data2.put("Longitude", location.getLongitude());
callback.invoke(data2);
}
}
配置相应的权限,在WXApplication中加入
WXSDKEngine.registerModule("getLocation", GetLocation.class);
然后就是前端调用
const getLocation= weex.requireModule('getLocation')
... //在方法中直接使用就行了
getLocation.getLocal(res => {
...
})
//获取到数据后可以取消监听
getLocation.removeLocalListener(res => {
...
})