【问题标题】:Getting the Current Location Lat Long as 0.0 in Android google maps在 Android 谷歌地图中获取当前位置 Lat Long 为 0.0
【发布时间】:2013-09-24 18:10:27
【问题描述】:

我想知道当前位置的纬度和经度。 它在模拟器上运行,但在移动设备上提供 lat/long 0.0。 我的设备是 LG 4x 高清。 src/.java 文件:

private TextView txt_lat,txt_lon,txt_gps;
private LocationManager lm;
double lat, lon;
protected LocationListener locatlist;
Location locat;
GpsStatus.Listener gpslist;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt_lat= (TextView) findViewById(R.id.textView2);
    txt_lon= (TextView) findViewById(R.id.textView4);
    txt_gps = (TextView) findViewById(R.id.textView5);
            lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            locat = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if(locat != null) {
                lat = locat.getLatitude();
                lon = locat.getLongitude();
            }
            locatlist = new LocationListener() {

                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderEnabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderDisabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onLocationChanged(Location location) {
                    // TODO Auto-generated method stub
                    lat = location.getLatitude();
                    lon = location.getLongitude();
                    txt_lat.setText("lat : "+lat);
                    txt_lon.setText("lon : "+lon);
                }
            };
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locatlist);
            if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                txt_gps.setText("GPS is started");
            }
            else if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                txt_gps.setText("GPS is non-start");
            }
            txt_lat.setText("lat : "+lat);
            txt_lon.setText("lon : "+lon);
} 

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

mainfest 权限:

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

lat long 在设备上永远是 0.0,但它在模拟器上工作。 它是一个简单的代码,为什么它不起作用? 我哪里错了?

【问题讨论】:

    标签: android gps google-maps-android-api-2 android-maps-v2


    【解决方案1】:
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);-->may be here you get null value of location manager replace with this line  
    lm = (LocationManager) 
                context.getSystemService(Context.LOCATION_SERVICE);
    

    【讨论】:

    • 它不起作用。我尝试替换然后无法在模拟器和设备上运行
    猜你喜欢
    • 2021-09-11
    • 1970-01-01
    • 2014-02-19
    • 2012-11-26
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多