为了账号安全,请及时绑定邮箱和手机立即绑定

使用Mapsactivity时遇到错误(新手)

使用Mapsactivity时遇到错误(新手)

万千封印 2021-05-06 18:54:27
iam无法获取我的位置,我不知道为什么ive我在google上尝试了一切,因为iam是android的新手。请帮我解决这个问题package com.parkaspot.najeeb.project;import java.io.IOException;import java.util.ArrayList;import java.util.concurrent.TimeUnit;public class MainActivity extends AppCompatActivity {final String TAG = "MAIN";ArrayList<RecentData> list;RecentAdapter recentAdapter;LinearLayoutManager linearLayoutManager;RecyclerView recyclerView;Drawer result;Toolbar toolbar;Double lat, lng;Location location;SharedPreferences sharedPreferences, sharedPreferences1;String name, password;SpotsDialog dialog;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    dialog = new SpotsDialog(this,"Loading...", R.style.Loading);    sharedPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);    sharedPreferences1 = getSharedPreferences("userDetails", Context.MODE_PRIVATE);    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    boolean networkEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);    if (networkEnabled) {        Log.d(TAG,"network is enabled"); //shows successfully        if ((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {              Log.d(TAG,"permission ******************granted"); //This log Tag Never appears in logcat            return;        }        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);        Log.d(TAG, "Location is initialized"); //shows successfully    }这总是返回null请从头开始帮助我,我不知道如何解决它,我对android非常非常陌生,这是我的大学家庭工程项目,即使是使用gps或net provider也无法获得贷款。**请仅在这件事上帮助我,然后我的项目将完成。返回位置为NullIam a Newbie;请以更改的形式回复我的整个代码。request(Noob)非常感谢您,先生
查看完整描述

1 回答

?
慕容森

TA贡献1853条经验 获得超18个赞

为了获取位置,您需要使用requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)位置管理器上的方法来请求位置更新。


创建一个将打印数据的侦听器,例如:


requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, new LocationListener(){  

    @Override 

    public void onLocationChanged(Location location){

        Log.d("LOCATION", location.toString());

    }

});

这将要求位置管理器每1000毫秒(1s)为我提供一次,如果我从上一个位置移走了10m,则提供了一个新位置。


查看完整回答
反对 回复 2021-05-19
  • 1 回答
  • 0 关注
  • 156 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信