2014-04-28 117 views
0

我正在开发android应用程序,它会告知用户他(哪个楼)在哪里,gps工作正常,但它不准确,为什么我在我的代码中使用范围,因为建筑物非常靠近彼此之间,但在34楼使用许多IF ELSE是没有效率的。 还有其他方法吗?android gps坐标比较

这是我的代码。 package com.example.gps;

import java.util.ArrayList; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

Button btnShowLocation; 

// GPSTracker class 
GPSTracker gps,gps2; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    btnShowLocation = (Button) findViewById(R.id.btnShowLocation); 

    // show location button click event 
    btnShowLocation.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) {   
      // create class object 
      gps = new GPSTracker(MainActivity.this); 

      // check if GPS enabled  
      if(gps.canGetLocation()){ 


       double latitude = gps.getLatitude(); 
       double longitude = gps.getLongitude(); 
       if((latitude >=21.4886 && latitude <=21.4893)&& (longitude >=39.2454 && longitude <=39.2459)) 
       // \n is for new line 
       //Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
       Toast.makeText(getApplicationContext(), "Your are in building 7 " , Toast.LENGTH_LONG).show(); 
       else 
        if((latitude >=21.48899 && latitude <=21.4899)&& (longitude >=39.2456 && longitude <=39.2459)) 

        Toast.makeText(getApplicationContext(), "Your are in building 8 " , Toast.LENGTH_LONG).show(); 

        //} 
      }else{ 
       // can't get location 
       // GPS or Network is not enabled 
       // Ask user to enable GPS/network in settings 
       gps.showSettingsAlert(); 
      } 

     } 
    }); 
} 

}

回答

0

其更好地使用的LocationManager和LocationListener的

0

您可以使用minumum距离算法

link