簡體   English   中英

Android Geolocation 在模擬器中工作但在手機中不起作用

[英]Android Geolocation working in emulator but not working in phone

我做了一個簡單的應用程序與該地理位置顯示在文本視圖的用戶的當前位置,如緯度,經度和與地理編碼城市名COUNTRYNAME郵政代碼等

在模擬器中一切正常,但由於某種原因,我的手機中沒有檢索到該位置。

模擬器運行的是 android 7.1,我的手機運行的是 android 7.0,但這應該不是問題,因為我在制作應用程序時考慮了 6.0 棉花糖。

這是代碼

“主要活動”

package com.example.slimshady.LocationInfoApp;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    LocationManager locationManager;
    LocationListener locationListener;
    Geocoder geocoder;
    TextView latitude, longitude, city, postcode, country;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        latitude = (TextView)findViewById(R.id.latitude);
        longitude = (TextView) findViewById(R.id.longitude);
        city = (TextView)findViewById(R.id.city);
        postcode = (TextView)findViewById(R.id.postcode);
        country = (TextView)findViewById(R.id.country);


        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

                updateLocationInfo(location);

            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        };

        // asking permission starts here
        if (Build.VERSION.SDK_INT < 23){

            // api level lower than 23 so no need to ask for permission

            try {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, locationListener);

            }
            catch (SecurityException e){

                e.printStackTrace();
            }
        }
        else {

            // api level greater than or equal to 23 checking if has permission if not VVVV this condition
            // means go ask for permission

            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){

                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 0);
            }
            else{

                // means api level greater than or equal to 23 and already has permission
                // so no need to go out and ask for permission

                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, locationListener);

                // last known location because we went to this part means we have been here before
                Location lastKnownLocation =  locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (lastKnownLocation != null) {
                    updateLocationInfo(lastKnownLocation);
                }



            }

        }
        // asking permission ends here


    }

    public void updateLocationInfo(Location location) {

        geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());

        try {

            List<Address> locationAddress = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(),1);
            if (locationAddress != null && locationAddress.size() > 0) {

                latitude.setText("Latitude: "+String.valueOf(locationAddress.get(0).getLatitude()));
                longitude.setText("Longitude: "+String.valueOf(locationAddress.get(0).getLongitude()));
                city.setText("City: "+String.valueOf(locationAddress.get(0).getLocality()));
                postcode.setText("Post Code: "+String.valueOf(locationAddress.get(0).getPostalCode()));
                country.setText("Country: "+String.valueOf(locationAddress.get(0).getCountryName()));

            }


        }
        catch (Exception e){

        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

            startListening();

        }
    }

    public void startListening(){

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, locationListener);

        }

    }
}

代碼簡單而基本,一切都應該可以正常工作,但由於某種原因它沒有。

我的意思是我的物理設備中的 GPS 接收器可以與谷歌地圖配合使用,所以我知道它沒有壞。

看看它在模擬器中工作正常它在模擬器中工作的圖像

但是相同的代碼在我的手機上不起作用,我添加了所有權限:

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

看到同一個應用程序在我的物理設備上不起作用,這是我的物理設備的屏幕截圖

無法在移動設備上運行的屏幕截圖

如您所見,該位置已打開,並且在彈出窗口中詢問時我已授予權限。 最后但並非最不重要的一點是要表明 GPS 接收器可以正常工作並且在我的物理設備中沒有損壞。

我使用谷歌地圖的截圖我使用谷歌地圖的截圖 我用紅色圈出了 GPS,地圖可以找到我的 GPS 位置

如果您在物理設備中使用已發布的 apk,則必須在 Google API 控制台的限制部分中為發布 apk 添加 SHA1。 我認為它有幫助。

如果沒有,請通過在物理設備中調試應用程序來顯示您的 logcat 錯誤。

您的設備可以通過多種方式定位自身。 當它們不是“GPS”時,它們經常被混淆為“GPS”。

  1. GPS(全球定位系統)這是一種全球導航衛星系統 (GNSS),它依靠來自軌道衛星星座的信號來定位設備。 GPS 特指美國國防部部署的衛星星座。 替代方案包括 GLONASS(俄語)和 Galileo(歐洲)。
  2. 網絡這使用 WiFi 或藍牙信號來定位您的設備。 該設備掃描發射器 ID,然后通過其 Internet 連接將這些 ID 發送到服務器以在數據庫中查找。 服務器響應一個位置。

您的代碼目前僅使用LocationManager.GPS_PROVIDER ,它僅使用衛星來定位您的設備。 谷歌地圖可能正在使用網絡位置,當 GPS 被禁用時它仍然可以工作。

我懷疑你需要:

  1. 添加LocationManager.NETWORK_PROVIDER
  2. 在您的設備上啟用 GPS。 這是在設置和位置下的某個地方。

在此處輸入圖片說明

我解決了像你這樣的問題完全刪除了位置管理器,並根據FusedLocationProviderClient使所有位置工作,而且我在帶有LocationSettingsRequest三星 s8 上有一些奇怪的錯誤,可以啟動默認對話框以打開 gps,它與錯誤一起工作,但在刪除locationManager.requestLocationUpdates它與 fused 完美配合。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM