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

如何在Android中获取当前位置

如何在Android中获取当前位置

胡说叔叔 2019-12-18 12:12:10
如何在Android中获取当前位置我在使用Android定位系统的网络提供商获取当前的位置坐标时遇到了麻烦。已经阅读了很多教程,并为我的项目实现了4或5个现有的类,它们都给了我最后的坐标,而不是当前的坐标。我很确定这个问题是我错过的基本问题,但我无法理解它到底是什么。我现在使用的密码:这是我的主要活动package com.example.locationtests;import android.os.Bundle;import android.app.Activity;import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     GPSTracker mGPS = new GPSTracker(this);     TextView text = (TextView) findViewById(R.id.texts);     if(mGPS.canGetLocation ){     mGPS.getLocation();     text.setText("Lat"+mGPS.getLatitude()+"Lon"+mGPS.getLongitude());     }else{         text.setText("Unabletofind");         System.out.println("Unable");     }}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {     // Inflate the menu; this adds items to the action bar if it is present.     getMenuInflater().inflate(R.menu.main, menu);     return true;}}这是我用来跟踪的类:package com.example.locationtests;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface; import android.content.Intent;import android.location.Location;import android.location.LocationListener; import android.location.LocationManager;import android.os.Bundle;import android.provider.Settings; import android.util.Log;public final class GPSTracker implements LocationListener {     private final Context mContext;     // flag for GPS status     public boolean isGPSEnabled = false;     // flag for network status     boolean isNetworkEnabled = false;     // flag for GPS status     boolean canGetLocation = false;实际上,GPS定位工作得很好,但网络定位却不行。当我移动时,当我打开设备时,坐标上的GPS一直在变化,但当我关闭它并在NetworkProvider上中继时,情况就不一样了。
查看完整描述

3 回答

?
忽然笑

TA贡献1806条经验 获得超5个赞

首先,您需要定义一个LocationListener处理位置更改。

private final LocationListener mLocationListener = new LocationListener() {
    @Override
    public void onLocationChanged(final Location location) {
        //your code here
    }};

那就去拿LocationManager并要求提供位置更新

@Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME,
            LOCATION_REFRESH_DISTANCE, mLocationListener);}

最后确保你在“宣言”上添加了权限,

要使用仅基于网络的位置,请使用此一

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

对于基于GPS的定位,这个

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>



查看完整回答
反对 回复 2019-12-19
?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

您需要在位置变化方法,因为当位置发生更改时,将调用此方法。..也就是说,如果调用getLocation,则需要保存新位置以返回它。

如果您不使用位置变化永远都是老地方。



查看完整回答
反对 回复 2019-12-19
?
繁华开满天机

TA贡献1816条经验 获得超4个赞

我在用本教程而且它对我的应用程序也很有效。

在我的活动中,我写了以下代码:

GPSTracker tracker = new GPSTracker(this);
    if (!tracker.canGetLocation()) {
        tracker.showSettingsAlert();
    } else {
        latitude = tracker.getLatitude();
        longitude = tracker.getLongitude();
    }

还请检查您的模拟器是否使用GoogleAPI运行。



查看完整回答
反对 回复 2019-12-19
  • 3 回答
  • 0 关注
  • 415 浏览

添加回答

举报

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