0

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

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

4 回答 4

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 上的点击事件,请在 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) {
        }
}
于 2012-07-10T09:45:30.427 回答
0

更改 requestLocationUpdates 方法,使其更新更快且坐标变化更少。将其更改为:-

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

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

于 2012-07-10T09:45:31.880 回答
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) {

    }
};
于 2012-07-10T09:48:39.150 回答
0

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

于 2012-07-10T09:52:31.247 回答