2

我正在开发基于位置的项目,我正在使用以下代码

我正在为该项目使用 google api 8

 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    currloc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    TextView t = (TextView)findViewById(R.id.textView1);
    try{
        t.setText("Your current location is - "+currloc.getLatitude()+","+currloc.getLongitude());
    }catch (Exception e) {
        // TODO: handle exception
        t.setText("cant find current location ");
    }

这段代码在我的银河选项卡上也能正常工作,即使在 HTC 上也是如此

但是当我使用 nexus 时,它会返回位置的空值。我需要更改我的api级别还是对galaxy nexus有任何特定要求提前谢谢你:)

4

1 回答 1

3

请按照代码行..

Step1: into your oncreate

LocationListener locationListener = new LocalLocationListener();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

Step2: into class body

/**Listener on location change*/
private class LocalLocationListener implements LocationListener
{

    public void onLocationChanged(Location location)
    {
        String text = "My current Location is: "+location.getLatitude()+", "+location.getLongitude();
        GeoPoint geoPoint = new GeoPoint((int)(location.getLatitude()* 1E6), (int)(location.getLatitude() * 1E6));
        mapController_.animateTo(geoPoint);
        Toast.makeText(LocalMap.this, text, Toast.LENGTH_SHORT).show();
        Log.i("onLocationChanged", text);

    }

    public void onProviderDisabled(String provider)
    {
        // TODO Auto-generated method stub
        Toast.makeText(LocalMap.this, "GPS Disable", Toast.LENGTH_SHORT).show();
        Log.i("onProviderDisabled", "GPS Disable");
    }

    public void onProviderEnabled(String provider)
    {
        // TODO Auto-generated method stub
        Toast.makeText(LocalMap.this, "GPS Enable", Toast.LENGTH_SHORT).show();
        Log.i("onProviderEnabled", "GPS Enable");
    }

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

    }
于 2012-05-21T08:00:11.810 回答