我使用Android服务位置管理器创建了一个Android应用程序。其中我使用基于网络的位置管理来获取位置。应用程序在印度运行良好,并且它提供了正确的位置,但当印度以外的一些朋友(如美国,英国)运行此应用程序时,我得到错误的坐标指向中国或哈萨克斯坦。我无法理解这个问题。如果有人遇到过这个问题,请告诉我。
提前致谢。
代码如下:
//changes - start for fused location
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
//import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
//changes end for fused location with one comment above library
public class LocationService extends Service
{
//public static final String BROADCAST_ACTION = "Hello World";
private static final int TWO_MINUTES = 1000 * 60 * 2;
public LocationManager locationManager;
public MyLocationListener listener;
public Location previousBestLocation = null;
public String userMobile;
Intent intent;
int counter = 0;
@Override
public void onCreate()
{
super.onCreate();
}
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnected();
}
@Override
public int onStartCommand(Intent intent,int flag, int startId)
{
try {
if (isOnline()) {
try {
int time = 2 * 60 * 1000;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, time, 0, listener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, 0, listener);
} catch (Exception e) {
String error = e.getMessage();
}
}
return Service.START_STICKY;
} catch (Exception e){
stopService(new Intent(LocationService.this,LocationService.class));
return Service.START_STICKY;
}
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be worse
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return true;
}
return false;
}
/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
@Override
public void onDestroy() {
// handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
//Log.v("STOP_SERVICE", "DONE");
locationManager.removeUpdates(listener);
}
public static Thread performOnBackgroundThread(final Runnable runnable) {
final Thread t = new Thread() {
@Override
public void run() {
try {
runnable.run();
} finally {
}
}
};
t.start();
return t;
}
public class MyLocationListener implements LocationListener
{
class RequestTask extends AsyncTask<String, String, String> {
int timeOut = 0;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... uri) {
String responseString = null;
if(isOnline()) {
///do something....
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
public void onLocationChanged(final Location loc)
{
if(isBetterLocation(loc, previousBestLocation)) {
Double latitude = loc.getLatitude();
Double longitude = loc.getLongitude();
String mainUrl = "www.xyz.com";
Uri.Builder builder = new Uri.Builder();
float invAccuracy = loc.getAccuracy();
float accuracy = 0;
if(invAccuracy > 0.0){
accuracy = 100 - invAccuracy;
}
builder.scheme("http")
.authority(mainUrl)
.appendQueryParameter("acc",Float.toString(accuracy))
.appendQueryParameter("lt",latitude.toString())
.appendQueryParameter("ln",longitude.toString());
String urlStr = builder.build().toString();
new RequestTask().execute(urlStr);
}
}
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)
{
}
}
}