亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在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 關注
  • 493 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號