如何在Android中获取当前位置

如何在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上中继时,情况就不一样了。



胡说叔叔
浏览 421回答 3
3回答

忽然笑

首先,您需要定义一个LocationListener处理位置更改。private&nbsp;final&nbsp;LocationListener&nbsp;mLocationListener&nbsp;=&nbsp;new&nbsp;LocationListener()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;@Override &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;onLocationChanged(final&nbsp;Location&nbsp;location)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//your&nbsp;code&nbsp;here &nbsp;&nbsp;&nbsp;&nbsp;}};那就去拿LocationManager并要求提供位置更新@Overrideprotected&nbsp;void&nbsp;onCreate(Bundle&nbsp;savedInstanceState)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;super.onCreate(savedInstanceState); &nbsp;&nbsp;&nbsp;&nbsp;mLocationManager&nbsp;=&nbsp;(LocationManager)&nbsp;getSystemService(LOCATION_SERVICE); &nbsp;&nbsp;&nbsp;&nbsp;mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,&nbsp;LOCATION_REFRESH_TIME, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LOCATION_REFRESH_DISTANCE,&nbsp;mLocationListener);}最后确保你在“宣言”上添加了权限,要使用仅基于网络的位置,请使用此一<uses-permission&nbsp;android:name="android.permission.ACCESS_COARSE_LOCATION"/>对于基于GPS的定位,这个<uses-permission&nbsp;android:name="android.permission.ACCESS_FINE_LOCATION"/>

慕桂英3389331

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

繁华开满天机

我在用本教程而且它对我的应用程序也很有效。在我的活动中,我写了以下代码:GPSTracker&nbsp;tracker&nbsp;=&nbsp;new&nbsp;GPSTracker(this); &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!tracker.canGetLocation())&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tracker.showSettingsAlert(); &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;latitude&nbsp;=&nbsp;tracker.getLatitude(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;longitude&nbsp;=&nbsp;tracker.getLongitude(); &nbsp;&nbsp;&nbsp;&nbsp;}还请检查您的模拟器是否使用GoogleAPI运行。
打开App,查看更多内容
随时随地看视频慕课网APP