【问题标题】:Not getting current latitude and longitude没有得到当前的纬度和经度
【发布时间】:2014-03-16 07:41:21
【问题描述】:

在我的应用程序中,我没有得到当前的纬度和经度

public class MainActivity extends Activity implements
    LocationListener {
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView txtLat;
String lat;
String provider;
protected String latitude, longitude;
protected boolean gps_enabled, network_enabled;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.google_map_send_my_current_latlong);
    txtLat = (TextView) findViewById(R.id.textView1);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, this);
}

@Override
public void onLocationChanged(Location location) {
    txtLat = (TextView) findViewById(R.id.textView1);
    txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:"
            + location.getLongitude());
}

@Override
public void onProviderDisabled(String provider) {
    Log.d("Latitude", "disable");
}

@Override
public void onProviderEnabled(String provider) {
    Log.d("Latitude", "enable");
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d("Latitude", "status");
}
   }

我添加了我的清单

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

我想在 TextView 中显示我当前的经纬度。我在真实设备上检查过

【问题讨论】:

  • 获取 GPS 锁定需要时间。它可以是几秒钟到几分钟(或者如果你在室内,则永远不会)。如果 GPS 关闭,它也将无法工作。您是否在开启 GPS 的情况下在户外(谷歌地图工作的地方)尝试过?
  • 我的 GPS 已开启,我检查了大约两分钟。
  • 通知栏中的 GPS 符号 - 是闪烁还是常亮?如果它正在闪烁,则说明您没有锁。 (如果它根本不存在,那就是另一个问题了)。
  • 一直在闪烁
  • 这意味着您没有 GPS 锁。试着走出去,远离高楼。你的代码在我看来是正确的,但来自 GPS 的卫星信号是我们无法控制的

标签: android google-maps location


【解决方案1】:

我正在提供简单有效的代码来获取当前位置的纬度和经度,并且会给出最好的结果;首先它会看到卫星位置而不是 INTERNET 位置而不是 Sim 位置,如果没有找到任何东西,它会给出消息......它也会给出移动位置:

package selecom.loc;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.TextView;
//don't implents LocationListner if you wants only latitude and longitude 
//only at a time

public class MainActivity extends Activity implements LocationListener{
LocationManager locationManager;
TextView tvLatitude, tvLongitude;
String provider;
Location location;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tvLongitude=(TextView)findViewById(R.id.lng);
    tvLatitude=(TextView)findViewById(R.id.lat);

    locationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    Criteria c=new Criteria(); 
    //if we pass false than
    //it will check first satellite location than Internet and than Sim Network
    provider=locationManager.getBestProvider(c, false);
    location=locationManager.getLastKnownLocation(provider);
    if(location!=null)
    {
        double lng=location.getLongitude();
        double lat=location.getLatitude();
        tvLongitude.setText(""+lng);
        tvLatitude.setText(""+lat);
    }
    else
    {
        tvLongitude.setText("No Provider");
        tvLatitude.setText("No Provider");
    }
}
 //The below methods can be removed if you don't want location on move
@Override
public void onLocationChanged(Location arg0) 
{
    double lng =location.getLongitude();
    double lat=location.getLatitude();
    tvLongitude.setText(""+lng);
    tvLatitude.setText(""+lat);   
}
@Override
public void onProviderDisabled(String arg0) {  
}
@Override
public void onProviderEnabled(String arg0) {   
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
 }
}

【讨论】:

    【解决方案2】:

    在你的 onCreate() 方法中调用这个方法并检查它是否适合你

    public Location getLocation(){
    
        try {
            locMan=(LocationManager)mContext.getSystemService(LOCATION_SERVICE);
            isGPSEnabled=locMan.isProviderEnabled(LocationManager.GPS_PROVIDER);
            isNetworkEnabled=locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
            if(isGPSEnabled||isNetworkEnabled)
            {
                canGetLocation=true;
                if(isNetworkEnabled)
                {
                    locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, DIST,this);
                    if(locMan!=null)
                    {
                        loc=locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if(loc!=null)
                        {
                            Toast.makeText(mContext, "loac", Toast.LENGTH_SHORT).show();
                            latitude=loc.getLatitude();
                            longitude=loc.getLongitude();
                            LocationLogic.getInstance().setLat(latitude);
                            LocationLogic.getInstance().setLang(longitude);
    
                        }
                    }
    
                }
                if(isGPSEnabled&&loc==null)
                {
                    locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, DIST,this);
                    if(locMan!=null)
                    {
                        loc=locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if(loc!=null)
                        {
                            Toast.makeText(mContext, "loac", Toast.LENGTH_SHORT).show();
                            latitude=loc.getLatitude();
                            longitude=loc.getLongitude();
                            LocationLogic.getInstance().setLat(latitude);
                            LocationLogic.getInstance().setLang(longitude);
                        }
                    }
    
                }
            }
    
        double d=distTo(latitude, longitude, 31.3256, 75.5792);
        setDist(d);
        //  doReverseGeoCodingTask(loc);
            Log.i("done","done");
    
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    
        return loc;
    
    }
    

    在你的清单文件中添加这些权限:

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

    希望这对你有用。确保您已打开 GPS 或互联网。

    【讨论】:

      猜你喜欢
      • 2014-11-08
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多