2 baohanqing baohanqing 于 2014.07.03 08:34 提问

loaction在真机当中获取不到经纬度,但是虚拟机DDMS当中却可以

package com.example.locationtest;

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.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

LocationManager lm;
TextView show;

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

    lm=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
    show=(TextView) super.findViewById(R.id.show);

    //从GPS获取最近的最近定位信息
    Location loaction=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    Log.e("location", loaction+"");

    //更新界面显示
    updateView(loaction);

    //lm.setTestProviderEnabled("gps",true); 

    //周期性的获取定位信息,并触发监听器
    /**
     * provider
     * 最小时间
     * 距离
     */
    //每隔3s更新一下数据
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 8, new LocationListener() {

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {


        }

        @Override
        public void onProviderEnabled(String provider) {

            updateView(lm.getLastKnownLocation(provider));
        }

        @Override
        public void onProviderDisabled(String arg0) {

            updateView(null);
        }

        @Override
        public void onLocationChanged(Location loaction) {
            updateView(loaction);
        }
    });


}


//更新EditText当中的内容
public void updateView(Location loaction){

    if(loaction!=null){
        StringBuilder sb=new StringBuilder();

        Log.e("经度", loaction.getLongitude()+"");

        sb.append("实时位置信息:");
        sb.append("\n经度:"+loaction.getLongitude());
        sb.append("\n维度:"+loaction.getLatitude());
        sb.append("\n高度:"+loaction.getAltitude());
        sb.append("\n速度:"+loaction.getSpeed());
        sb.append("\n方向:"+loaction.getBearing());

        show.setText(sb.toString());

    }
    else{
        show.setText("");
    }

}   

}

权限都已经加完了

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!