2013-01-19 70 views
3

在我的应用程序我能够使用GPS,但问题是,它总是让我相同的位置。每次我从这个位置检索位置时,它都会给我同样的价值。第一次它被取出,我无线上网。然后,我只带GPS出去了,即使我离开一英里远,地点也一样。我启用了3G,仍然是一样的。GPS让我只有一个位置,永远不会改变Android

我把我的应用程序发送给我的一个朋友,他得到了不同的第一次的位置,然后他看到相同的,无论他去哪里。

这里是我的代码:

public Location getLocation() { 
     try { 
      locationManager = (LocationManager) mContext 
        .getSystemService(LOCATION_SERVICE); 

      // getting GPS status 
      isGPSEnabled = locationManager 
        .isProviderEnabled(LocationManager.GPS_PROVIDER); 

      // getting network status 
      isNetworkEnabled = locationManager 
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

      if (!isGPSEnabled && !isNetworkEnabled) { 
       // no network provider is enabled 
      } else { 
       this.canGetLocation = true; 
       // First get location from Network Provider 
       if (isNetworkEnabled) { 
//     System.out.println("Network enabled"); 
        locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
       // if GPS Enabled get lat/long using GPS Services 
       if (isGPSEnabled) { 
//     System.out.println("GPS enabled"); 
        if (location == null) { 
         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("GPS Enabled", "GPS Enabled"); 
         if (locationManager != null) { 
          location = locationManager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
          } 
         } 
        } 
       } 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return location; 
    } 

@Override 
    public void onLocationChanged(Location location) { 
//  mAct.playToastMessage("Location Changed"); 
     getLocation(); 
//  mAct.setGPSText(location.getLatitude(),location.getLongitude()); 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

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

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 
+0

请检查[**此**](http://rdcworld-android.blogspot.in/2012/01/get-current-location-coordinates-city.html)我每次都得到新的GPS数据 – swiftBoy

回答

3
locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
         MIN_TIME_BW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

在本作中,你必须设置0和0。如果是MIN_TIME_BW_UPDATES和MIN_DISTANCE_CHANGE_FOR_UPDATES的参数设置为0,0。它会尽快为您提供位置更新。如果你设置了较大的值,你永远不会得到更新的值,直到你的条件满足。我希望这可以解决你的问题。

1
@Override 

public void onLocationChanged(Location loc) 

{ 

loc.getLatitude(); 

loc.getLongitude(); 

String Text = “My current location is: “ + 

“Latitude = “ + loc.getLatitude() + 

“Longitude = “ + loc.getLongitude(); 


Toast.makeText(getApplicationContext(),Text,Toast.LENGTH_SHORT).show(); 

} 

来源:Tutorial on GPS to get Current Location.