用户的当前位置未找到使用GPS提供商

时间:2012-07-10 09:39:52

标签: android gps

我使用以下代码查找用户的当前位置

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);

public void onLocationChanged(Location location) {
    Log.e("changed","location");
    // TODO Auto-generated method stub
    showLocation(location); 
}

但用户的位置并未找到。如果我将提供商更改为网络提供商,那么它的工作情况。但只有GPS提供商无法正常工作。

4 个答案:

答案 0 :(得分:0)

请您详细说明一下?您是否需要通过Long或Lat在mapview中找到当前位置?如果这是你的问题,那么做一件事: 通过DDMS手动发送或通过在mapview上单击生成Lat和Lang来查找Lat和Long: 它可以通过以下方式完成:

 public void onLocationChanged(Location loc) {
         loc.getLatitude();
     loc.getLongitude();
        String Text = "My current location is: " + "Latitud = " + loc.getLatitude() + "Longitud = " + loc.getLongitude();
    Toast.MakeTest(getApplicationContext(),Text,Toast.Length_Long).show()
  }

所以通过这个你可以得到你的lat和Long。在mapView的点击监听器上获得Lat和Long之后你可以写下面的代码:

    Projection proj = view1.getProjection();
  GeoPoint loc = proj.fromPixels((int)ev.getX(), (int)ev.getY()); 
   //Getting Lat and Log 
 String longitude =  Double.toString(((double)loc.getLongitudeE6())/1000000);
 String latitude = Double.toString(((double)loc.getLatitudeE6())/1000000);
  GeoPoint point = new GeoPoint(  (int) (loc.getLatitudeE6()),(int) (loc.getLongitudeE6()));
   //Getting location from lat and Long
   String address="";
    Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
     try {
     List<Address> addresses = geoCoder.getFromLocation(point.getLatitudeE6()  / 1E6,     
    point.getLongitudeE6() / 1E6, 1);
    if (addresses.size() > 0) {
  for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
    address += addresses.get(0).getAddressLine(index) + " ";
     }
     }
    catch (IOException e) {                
                              e.printStackTrace();
                          }   
      Toast t =Toast.makeText(getApplicationContext(), "Longitude: "+ longitude +"  Latitude: "+ latitude+" name: "+address, Toast.LENGTH_LONG);
      t.show();

点击mapview即可获得输出。 对于MapView上的click事件,请在dispatchTouchEvent()中将上面的代码写为

    public boolean dispatchTouchEvent(MotionEvent ev) {
    int actionType = ev.getAction();
    switch (actionType) {
        case MotionEvent.ACTION_UP:
         //write above code here
              }
          }  

试试这个。 编辑:使用下面给出的代码检查您的代码。

    public class MyLocationListener implements LocationListener {
    public void onLocationChanged(Location loc) {
        loc.getLatitude();
        loc.getLongitude();
        String Text = "My current location is: " + "Latitud = "
                + loc.getLatitude() + "Longitud = " +    loc.getLongitude();
       latituteField.setText(String.valueOf(loc.getLatitude()));
   longitudeField.setText(String.valueOf(loc.getLongitude()));
    }

       public void onProviderDisabled(String provider) {
   Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
    }
     public void onProviderEnabled(String provider) {
 Toast.makeText(getApplicationContext(), "Gps Enabled",Toast.LENGTH_SHORT).show();
    }
    public void onStatusChanged(String provider, int status, Bundle extras) {
        }
}

答案 1 :(得分:0)

更改requestLocationUpdates方法,以便更快地更新并减少坐标更改。将其更改为: -

requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

立即尝试。如果可能的话,将手机移动几米,以便GPS卫星很快检测到它。我假设GPS已开启。

答案 2 :(得分:0)

首先检查您的GPS是启用还是禁用。启用GPS后,使用位置监听器获取当前位置。我正在使用以下代码获取GPS位置

String sourceLat, sourceLong;
LocationManager locManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);        

    txtCurrLat = (TextView) findViewById(R.id.txtCurrLat);
    txtCurrLong = (TextView) findViewById(R.id.txtCurrLong);

    mapView = (MapView) findViewById(R.id.mapview);


    geoPoint = null;
    mapView.setSatellite(false);

    locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f, locationListener);
    Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if(location != null)                                
    {
        double fromLat = location.getLatitude();
        double fromLong = roundTwoDecimals(location.getLongitude());

        Toast.makeText(getApplicationContext(), fromLat +fromLong+""  , Toast.LENGTH_SHORT).show();
        sourceLat = Double.toString(fromLat);
        sourceLong = Double.toString(fromLong);

        txtCurrLat.setText(sourceLat+","+sourceLong);   

        btnShow.setOnClickListener(this);
    }else{

        Toast.makeText(getApplicationContext(), "Location is not avilable", Toast.LENGTH_SHORT).show();
    }





}

private void updateWithNewLocation(Location location) {

    String latLongString = "";
    if (location != null) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        sourceLat = Double.toString(lat);
        sourceLong = Double.toString(lng);
        latLongString =  sourceLat + "," + sourceLong;
    } else {
        latLongString = "No location found";
        Toast.makeText(getApplicationContext(), latLongString+"", Toast.LENGTH_SHORT).show();
    }
    txtCurrLat.setText(latLongString);
}

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider) {
        Toast.makeText( getApplicationContext(), "Gps is Disabled.Please enabale gps", Toast.LENGTH_SHORT ).show();
        updateWithNewLocation(null);
    }

    public void onProviderEnabled(String provider) {
        Toast.makeText( getApplicationContext(), "Gps is Enabled", Toast.LENGTH_SHORT).show();
    }

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

    }
};

答案 3 :(得分:0)

GPS不提供“按需获取我的位置”功能。您必须耐心等待,直到设备从多颗卫星获得修复。这可能需要几分钟,即使有清晰的天空视野。这就是操作系统将其实现为回调的原因。 onLocationChanged在设备有修复时运行,你只需要等待它。