找到我的位置不起作用(android 2.2)

时间:2011-12-13 14:16:01

标签: android google-maps location

我尝试实现此链接location find

中的代码

事实是我想使用网络或GPS获取我的位置(我在网络上测试过)

private Location getCurrentLocation(){
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.

        mLatitude = location.getLatitude();
        mLongitude =  location.getLongitude();
        myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
        mapController.animateTo(myGeoPoint);

    }

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

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
  };

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
String locationProvider = LocationManager.NETWORK_PROVIDER;
// Or use LocationManager.GPS_PROVIDER

Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
if(lastKnownLocation==null){
     locationProvider = LocationManager.GPS_PROVIDER;
    // Or use LocationManager.GPS_PROVIDER

     lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);    
}

return lastKnownLocation;

}

如何返回结果:

myLocation = getCurrentLocation();
        if(myLocation != null)
        {
        mLatitude = myLocation.getLatitude();
        mLongitude =  myLocation.getLongitude();
        myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
        mapController.animateTo(myGeoPoint);

        }else {         
            mLatitude =  36.859502;
            mLongitude = 10.168097;
            myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
            mapController.animateTo(myGeoPoint);}

这不是我尝试的唯一代码,我在Android 2.2手机上找不到任何结果。

关于如何解决它的任何想法

  

我在网上检查大部分教程!!

2 个答案:

答案 0 :(得分:1)

首先检查您是否添加了以下权限。

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

第二件事是你应该花点时间找一下位置信息。你可以使用计时器任务或posDelayed来做。

private Location getCurrentLocation(){
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
        t.cancel();
        mLatitude = location.getLatitude();
        mLongitude =  location.getLongitude();
        myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
        mapController.animateTo(myGeoPoint);

    }

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

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
  };

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Location lastKnownLocation;
Timer t = new Timer();

    t.schedule(new TimerTask() {
                @Override
                public void run() {
                           this.cancel();
                           lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
                           if(lastKnownLocation==null){
                                     locationProvider = LocationManager.GPS_PROVIDER;
                                     // Or use LocationManager.GPS_PROVIDER

                             lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);   
                             return lastKnownLocation;


             }

    },30000);

}

它将在30秒后给你结果。

答案 1 :(得分:0)

经过长时间的研究,我很高兴Here:)

希望这会对某人有所帮助。