当前位置在Android模拟器中不起作用

时间:2011-01-21 22:21:34

标签: android emulation

我正在按照教程获取当前位置。我写的正是教程所说的,但我只是没有得到我的lat / lon。我正在使用wifi连接。我试过了:
geo fix -77.036519 38.896143
仍然没有得到纬度/经度。我的代码如下;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.TextView;

public class TestProviderController extends Activity {
 LocationManager locationManager;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String location_context=Context.LOCATION_SERVICE;
        locationManager=(LocationManager)getSystemService(location_context);
        testProvider();
    }
    public void testProvider(){
     TextView tv = (TextView)findViewById(R.id.myTextView);
     StringBuilder sb = new StringBuilder("Enabled Providers:");

     List<String> providers = locationManager.getProviders(true);

     for (String provider : providers) {

     locationManager.requestLocationUpdates(provider, 1000, 0,
     new LocationListener() {
     public void onLocationChanged(Location location) {}
     public void onProviderDisabled(String provider){}

     public void onProviderEnabled(String provider){}
     public void onStatusChanged(String provider, int status,
     Bundle extras){}
     });
     sb.append("\n").append(provider).append(":");
     Location location = locationManager.getLastKnownLocation(provider);
     if (location != null) {
     double lat = location.getLatitude();
     double lng = location.getLongitude();
     sb.append(lat).append(",").append(lng);
     } else {
     sb.append("No Location");
     }
     }
     tv.setText(sb);
    }
}

有人可以帮忙解决问题吗?

1 个答案:

答案 0 :(得分:0)

显然需要一点时间来获取坐标 - 这是指向类似question的链接