在位置经度和纬度中获取空值

Posted

技术标签:

【中文标题】在位置经度和纬度中获取空值【英文标题】:Getting null in Location Longitude and Latitude 【发布时间】:2018-07-20 14:12:01 【问题描述】:

我的经度和纬度变量为空。

GpsTracker.java

package com.mymate.myownidea.fawad.my_mate;

import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import static android.content.Context.LOCATION_SERVICE;

/**
 * Created by shiva on 8/4/17.
 */

public class GpsTracker implements LocationListener 

    Context context;

    public GpsTracker(Context context) 
        super();
        this.context = context;
    

    public Location getLocation()
        if (ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED) 
            Log.e("fist","error");
            return null;
        
        try 
            LocationManager lm = (LocationManager) context.getSystemService(LOCATION_SERVICE);
            boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
            if (isGPSEnabled)
                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000,10,this);
                Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                return loc;
            else
                Log.e("sec","errpr");
            
        catch (Exception e)
            e.printStackTrace();
        
        return null;
    

    @Override
    public void onLocationChanged(Location location) 
    

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) 
    

    @Override
    public void onProviderEnabled(String provider) 
    

    @Override
    public void onProviderDisabled(String provider) 
    

和 MainMenu.java

package com.mymate.myownidea.fawad.my_mate;

import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;



public class MainMenuActivity extends FragmentActivity 


    String user_type;

    Bundle bundle;

    Fragment newFragment;

    private FirebaseAuth mAuth;
    String uid;
    DatabaseReference rootRef;
    DatabaseReference uidRef;
    FirebaseUser currentFirebaseUser;

    public static String longitude;
    public static String latitude;
    String id;

    DatabaseReference databaseUser;


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


        ActivityCompat.requestPermissions(MainMenuActivity.this, new String[] 
                android.Manifest.permission.ACCESS_FINE_LOCATION, 123);

        GpsTracker gt = new GpsTracker(getApplicationContext());
        Location l = gt.getLocation();
        if( l == null)
            Toast.makeText(getApplicationContext(),"GPS unable to get Value",Toast.LENGTH_SHORT).show();
        else 
            double lat = l.getLatitude();
            double lon = l.getLongitude();
            Toast.makeText(getApplicationContext(),"GPS Lat = "+lat+"\n lon = "+lon,Toast.LENGTH_SHORT).show();
        


        Toast.makeText(getApplicationContext(), longitude + " " + latitude, Toast.LENGTH_SHORT).show();

      

我的经度和纬度为空。导入android的功能有一些错误或有某种类型的逻辑错误。我尝试了很多功能,但总是为空。或者我正在使用旧方法。

【问题讨论】:

【参考方案1】:

有时可能需要一段时间才能在手机上获取位置信息。所以调用getLastKnownLocation() 可能需要一段时间才能返回非空结果。

相反,您应该在onLocationChanged 方法中获取位置数据,该方法将在位置可用时调用。

【讨论】:

【参考方案2】:

创建接口

 interface IOnLocationChanged 
        void onLocationchanged(Location location);
    

GpsTracker

private  IOnLocationChanged iOnLocationChanged;

    public GpsTracker(Context context,IOnLocationChanged iOnLocationChanged) 
        super();
        this.context = context;
        this.iOnLocationChanged = iOnLocationChanged;
    

onLocationChanged

@Override
    public void onLocationChanged(Location location) 
       if(iOnLocationChanged!=null)
       iOnLocationChanged.onLocationchanged(location);
       
    

MainMenuActivity

public class MainMenuActivity extends FragmentActivity implements IOnLocationChanged

 @Override
    public void onLocationchanged(Location location) 
        //get Your location here
    


【讨论】:

以上是关于在位置经度和纬度中获取空值的主要内容,如果未能解决你的问题,请参考以下文章

使用当前位置时,我不断获取位置空值

给定纬度和经度,在 Sencha Touch 中获取位置名称

如何从纬度/经度获取附近的位置?

如何使用纬度和经度在Android中获取位置名称[重复]

使用gps从纬度和经度获取当前位置

如何在python中获取经度和纬度的位置?