我是一个新的android编程。我想获得curent用户位置并显示他/她附近的resturant.So我写了下面的代码获取android studio上的位置。但它工作不成功。我也使用了一个凌空图书馆在我的项目中从wordpress获取json信息。我不知道凌空库是否会干扰位置。我的代码是:
import configureMockStore from 'redux-mock-store';
import PageLoading from 'src/home/components/PageLoading';
const middlewares = [];
const mockStore = configureMockStore(middlewares);
Enzyme.configure({ adapter: new Adapter() });
describe("Page Loading",()=>{
it("testing shoul dispatch action on calling componentdid mount",()=>{
const initialState = {}
const store = mockStore(initialState)
const wrapper = mount(<PageLoading message="loading"/>);
const actions = store.getActions();
const expectedPayload = {type: 'SET_NOTIFICATION_DIALOG', status: '200', message:"loading", model: 'LOADING' };
expect(actions).toEqual([expectedPayload])
})
})
此方法适用于 cheack权限并调用 requestLocationUpdates 。
private void getlocationaddresss(){
progressBar.setVisibility(View.VISIBLE);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
listener=new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Geocoder geocoder = new Geocoder(MainActivity.this, new Locale("fa"));
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(), 1);
} catch (IOException e) {
e.printStackTrace();
}
cityName = addresses.get(0).getLocality();
stateName=addresses.get(0).getAdminArea();
progressBar.setVisibility(View.GONE);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(MainActivity.this,"لطفا لوکیشن خود را روشن نمایید",Toast.LENGTH_LONG).show();
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
};
configure_button();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 10:
configure_button();
break;
default:
break;
}
}
我该如何解决?
答案 0 :(得分:0)
您应该检查用户是否已开启位置以获取当前位置。 以下是您可以直接在app中使用的有用方法。
您可以在活动中查看此类内容。这指的是当前的活动背景。
$(".question__title").click(function() {
$(".question").addClass("question--open");
$(".question__title").click(function() {
$(".question").removeClass("question--open");
});
return false;
});
和
if (!isLocationEnabled(this)){
showLocationSettingsAlert(this);
return;
}
如果未开启,则启动位置设置活动
public static boolean isLocationEnabled(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return false;
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}