我的经度和纬度变量为空。
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函数时发生错误或发生某种逻辑错误。我已经尝试了许多功能,但始终为空。或者我使用的是旧方法。
答案 0 :(得分:0)
有时可能需要一段时间才能在手机上找到位置。因此,调用getLastKnownLocation()
可能需要一段时间才能返回非空结果。
相反,您应该使用onLocationChanged
方法获取位置数据,该数据将在位置可用时调用。
答案 1 :(得分:0)
创建界面
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
}
}