簡體   English   中英

Android,試圖找到邏輯來存儲用戶位置中的緯度和經度值

[英]Android, trying to find logic to store latitude and longitude values from user location

如問題中所述,我想將經度和緯度保存為雙值,以便可以在程序中使用它們。

我可以使用setOnMyLocationChange偵聽器,然后從我創建的UserLocation類中創建一個對象,該對象存儲經度和緯度。

我可以使用UserLocation.getLat()在偵聽器方法中將其打印出來,但是我無法存儲該變量以在程序的其他位置使用它。 我將發布我認為相關的代碼。

我有UserLocation l存儲為全局變量

UserLocation l;

盡管我可以在偵聽器中引用變量,但在這里我獲得了l.getLat()的空對象引用。

else if (city.equals("myLocation")) {

            map.setMyLocationEnabled(true);
            Location myLocation = map.getMyLocation();
            map.setOnMyLocationChangeListener(this);
//            map.setOnMyLocationChangeListener(null);
            lat = l.getLat();
            lon = l.getLng();
            Toast.makeText(this, lat+" is latMy in map activity ", Toast.LENGTH_LONG).show();


        }

在這里我可以打印出正確的值

  @Override
    public void onMyLocationChange(Location location) {

        latMy = location.getLatitude();
         lngMy = location.getLongitude();
        l = new UserLocation();
        l.setLat(latMy);
        l.setLng(lngMy);
        Toast.makeText(this, latMy+" is latMy in LOCATION CHANGE ", Toast.LENGTH_LONG).show();

    }

這是我的UserLocation類

package com.example.its.citymap;

public class UserLocation {
    private double lat; 
    private double lng; 

    public double getLat() {
        return lat;
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    public double getLng() {
        return lng;
    }

    public void setLng(double lng) {
        this.lng = lng;
    }
}

我已經搜索並嘗試了很多,但找不到可靠的答案。

謝謝

首先,我檢查啟用了哪些提供程序。 有些可能在設備上被禁用,有些可能在應用程序清單中被禁用。

如果有任何提供程序,我將啟動位置偵聽器和超時計時器。 在我的示例中,這是20秒,對於GPS來說可能還不夠,因此可以將其放大。

如果我從位置偵聽器獲取更新,則使用提供的值。 我停止監聽器和計時器。

如果我沒有任何更新且計時器已過去,則必須使用最近的已知值。

我從可用的提供程序中獲取最近的已知值,然后選擇其中的最新值。

這是我上課的方式:

LocationResult locationResult = new LocationResult(){
    @Override
    public void gotLocation(Location location){
        //Got the location!
    }
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
And here's MyLocation class:

import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class MyLocation {
    Timer timer1;
    LocationManager lm;
    LocationResult locationResult;
    boolean gps_enabled=false;
    boolean network_enabled=false;

    public boolean getLocation(Context context, LocationResult result)
    {
        //I use LocationResult callback class to pass location value from MyLocation to user code.
        locationResult=result;
        if(lm==null)
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        //exceptions will be thrown if provider is not permitted.
        try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
        try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}

        //don't start listeners if no provider is enabled
        if(!gps_enabled && !network_enabled)
            return false;

        if(gps_enabled)
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
        if(network_enabled)
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
        timer1=new Timer();
        timer1.schedule(new GetLastLocation(), 20000);
        return true;
    }

    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerNetwork);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerGps);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    class GetLastLocation extends TimerTask {
        @Override
        public void run() {
             lm.removeUpdates(locationListenerGps);
             lm.removeUpdates(locationListenerNetwork);

             Location net_loc=null, gps_loc=null;
             if(gps_enabled)
                 gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
             if(network_enabled)
                 net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

             //if there are both values use the latest one
             if(gps_loc!=null && net_loc!=null){
                 if(gps_loc.getTime()>net_loc.getTime())
                     locationResult.gotLocation(gps_loc);
                 else
                     locationResult.gotLocation(net_loc);
                 return;
             }

             if(gps_loc!=null){
                 locationResult.gotLocation(gps_loc);
                 return;
             }
             if(net_loc!=null){
                 locationResult.gotLocation(net_loc);
                 return;
             }
             locationResult.gotLocation(null);
        }
    }

    public static abstract class LocationResult{
        public abstract void gotLocation(Location location);
    }
}

暫無
暫無

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

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