GPS无法获取地理位置信息

echocao 发布于 2012/03/28 18:59
阅读 5K+
收藏 0
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
    /** Called when the activity is first created. */
LocationManager lm = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     // 获得LocationManager实例
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 使用GPS定位
Location location = lm
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//判断Location对象是否为空,为空,则使用网络定位
if(location == null){
location =
lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
// 更新
updateView(location);
    }
    /**
* 更新EditText中显示内容
* @param location
*/
public void updateView(Location location) {
String text = "";
if (location != null) {
// 获得纬度
double lat = location.getLatitude();
// 获得经度
double lng = location.getLongitude();
text = "经度:" + lng + "\n纬度:" + lat;
} else {
text = "无法获取地理信息!";
}
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(text);
}
} 
-------------------------------------------------------------------------------------------------------------
权限已经加过了,<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
运行不报错,Emulator control中已经send我的经纬度地址,latency已设置为GPRS。
加载中
0
echocao
echocao
不懂啊  纠结啊
0
yafeng
yafeng

lm.requestLocationUpdates("gps", 0, 0,new LocationListener(){

@Override
    public void onLocationChanged(Location location) {
       
TextView tv = (TextView) findViewById(R.id.tv);

           
tv.setText("纬度:"+location.getLatitude()+"经度:"+ location.getLongitude());


    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

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

    }

});

将以上代码放到 onCreate()方法里面,你再发送经纬度,就可以获取到了

返回顶部
顶部