得到错误的纬度和经度使用GPS

问题描述:

我用https://*.com/a/5672274/6881568找到抬高。我传递的经度和我使用GPS得到纬度值(其代码如下)得到错误的纬度和经度使用GPS

public class GPSTracker extends Service implements LocationListener { 
private final Context mContext; 

// flag for GPS status 
boolean isGPSEnabled = false; 

// flag for network status 
boolean isNetworkEnabled = false; 

boolean canGetLocation = false; 

Location location; // location 
double latitude; // latitude 
double longitude; // longitude 
double altitude; 

// The minimum distance to change Updates in meters 
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 

// The minimum time between updates in milliseconds 
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 

// Declaring a Location Manager 
protected LocationManager locationManager; 

public GPSTracker(Context context) { 
    this.mContext = context; 
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 
    getLocation(); 
} 

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) { 
       locationManager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 
         MIN_TIME_BW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
       Log.d("Network", "Network"); 
       if (locationManager != null) { 
        location = locationManager 
          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        if (location != null) { 
         latitude = location.getLatitude(); 
         longitude = location.getLongitude(); 
         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
         StrictMode.setThreadPolicy(policy); 
         altitude = getAltitude(latitude,longitude); 
        } 
       } 
      } 
      // if GPS Enabled get lat/long using GPS Services 
      if (isGPSEnabled) { 
       if (location == null) { 
        locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
          StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
          StrictMode.setThreadPolicy(policy); 
          altitude = getAltitude(latitude,longitude); 
         } 
        } 
       } 
      } 
     } 

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

    return location; 
} 

public double getLatitude(){ 
    if(location != null){ 
     latitude = location.getLatitude(); 
    } 
    // return latitude 
    return latitude; 
} 

/** 
* Function to get longitude 
* */ 
public double getLongitude(){ 
    if(location != null){ 
     longitude = location.getLongitude(); 
    } 

    // return longitude 
    return longitude; 
} 

public boolean canGetLocation() { 
    return this.canGetLocation; 
} 

/** 
* Function to show settings alert dialog 
* */ 
public void showSettingsAlert(){ 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 

    // Setting Dialog Title 
    alertDialog.setTitle("GPS is settings"); 

    // Setting Dialog Message 
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); 

    // Setting Icon to Dialog 
    //alertDialog.setIcon(R.drawable.delete); 

    // On pressing Settings button 
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog,int which) { 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      mContext.startActivity(intent); 
     } 
    }); 

    // on pressing cancel button 
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
     } 
    }); 

    // Showing Alert Message 
    alertDialog.show(); 
} 

public double getAltitude(Double longitude, Double latitude) { 

    double result = Double.NaN; 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpContext localContext = new BasicHttpContext(); 
    String url = "https://maps.googleapis.com/maps/api/elevation/" 
      + "xml?locations=" + String.valueOf(longitude) 
      + "," + String.valueOf(latitude) 
      + "&key=AIzaSyD4915zh6cSIB7BAUQI_WZWb-7EOjui8wg"; 
    HttpGet httpGet = new HttpGet(url); 

    try { 

     HttpResponse response = httpClient.execute(httpGet, localContext); 
     HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
      InputStream instream = entity.getContent(); 
      int r = -1; 
      StringBuffer respStr = new StringBuffer(); 
      while ((r = instream.read()) != -1) 
       respStr.append((char) r); 
      String tagOpen = "<elevation>"; 
      String tagClose = "</elevation>"; 

      if (respStr.indexOf(tagOpen) != -1) { 
       int start = respStr.indexOf(tagOpen) + tagOpen.length(); 
       int end = respStr.indexOf(tagClose); 
       String value = respStr.substring(start, end); 
       result = (double) (Double.parseDouble(value) *3.2808399);// convert from meters to feet 
      } 
      instream.close(); 
     } 
    } catch (ClientProtocolException e) { 
    } catch (IOException e) { 
    } 
    return result; 
} 

我得到错误的结果,当我在为我下面的建筑物的不同楼层进行测试,

5楼 - (纬度= 12.9174253,东经= 77.5005331,海拔=2636.495278941856英尺)

AT四楼 - (纬度= 12.9176438,东经= 77.5004883,高程=2634.1930440302203英尺)

AT三楼 - (纬度= 12.9175136,东经= 77.5005331,高程=2635.651439701728英尺)

这是不对的我在第4级第3级和2634获得2635。高度应在多4 level.at二楼,我再次得到了2636英尺

高程的API让你从地球表面的高度,但不考虑建筑物或室内。它给了你在那个地点的地球的高度,所以如果你在山顶或海平面上的价值会有所不同,但如果你去三楼或“跳”你不会得到“奇迹” “海拔。 该服务具有一种预先计算好的高程的“网格”,在您考虑的所有楼层中,完全相同位置的高程值应该相同。

+0

是什么抬高首先,我认为它的高度在海平面。什么是分辨率? (在谷歌高程API)。我无法理解在其网站 – praveen

+0

中给出的一个。其实我的要求是从地球表面找到高度或我的移动高度。 – praveen

+0

如果你没有在建筑物内的任何WiFi,蓝牙或其他参考你能做的唯一一件事就是尝试使用晴雨表,如果手机有它 –