0

ユーザーがログインする前に、ユーザーの現在の緯度と経度を計算する必要があります。モバイル デバイスでコードをテストしましたが、機能していないようです。これが私のコードです:

     LocationManager mlocManager=null; 
     LocationListener mlocListener=null;

     @Override
     public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);        
     mlocListener = new MyLocationListener();
     }

     @Override protected void onResume() {    
     super.onResume();    
     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, mlocListener);
     }

     @Override  protected void onPause() {    
     super.onPause();    
     mlocManager.removeUpdates(mlocListener); //<8>     
     } 

     /* Class My Location Listener */ 
     public class MyLocationListener implements LocationListener 
     { 

      @Override 
      public void onLocationChanged(Location loc) 
      {         
        Toast.makeText(getApplicationContext(),"In onchange", Toast.LENGTH_SHORT).show();
        if(loc!=null){                                                                  

            latitude=loc.getLatitude(); 
            longitude=loc.getLongitude(); 

            if(loc.getLatitude()!=0.0 || loc.getLongitude()!=0.0){       

                Toast.makeText(getApplicationContext(),"Location not null",    Toast.LENGTH_SHORT).show();
                SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);     
                SharedPreferences.Editor e = prefsSaveLatLong.edit();
                e.remove("LAT");
                e.remove("LONG");
                e.putString("LAT",Double.toString(loc.getLatitude()));
                e.putString("LONG",Double.toString(loc.getLongitude()));
                e.commit();

                String Text = "My current location is: " + "Latitude = " +   loc.getLatitude() + "Longitude = " + loc.getLongitude(); 
                Toast.makeText(getApplicationContext(),Text+" "+latitude+" "+longitude, Toast.LENGTH_SHORT).show();

            }else{

                SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);
                if(prefsSaveLatLong.contains("LAT") && prefsSaveLatLong.contains("LONG")){

        SharedPreferences.Editor e1 = prefsSaveLatLong.edit();
                    e1.remove("LAT");
                    e1.remove("LONG");
                    e1.commit();
                }
            }
            // set latitude longitude to label
            setLatLongLabel();

        }else{
            latLongLabel.setTextColor(Color.parseColor("#FF0000"));
            latLongLabel.setText("Latitude-Longitude not available");

            SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);
            if(prefsSaveLatLong.contains("LAT") && prefsSaveLatLong.contains("LONG")){
                SharedPreferences.Editor e1 = prefsSaveLatLong.edit();
                e1.remove("LAT");
                e1.remove("LONG");
                e1.commit();
            }
        }
    } 

    @Override 
    public void onProviderDisabled(String provider) 
    { 
        gpsEnabled=false;               
        if(!gpsEnabled){            
             Toast.makeText( getApplicationContext(),"Gps Disabled", Toast.LENGTH_SHORT ).show();
            showSettingsAlert();
        }    
    } 

    @Override 
    public void onProviderEnabled(String provider) 
    { 
        gpsEnabled=true;        
        Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
        if(status==0){
            Toast.makeText(getApplicationContext(),"OUT_OF_SERVICE",Toast.LENGTH_SHORT).show();
            SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);
            if(prefsSaveLatLong.contains("LAT") && prefsSaveLatLong.contains("LONG")){
                SharedPreferences.Editor e1 = prefsSaveLatLong.edit();
                e1.remove("LAT");
                e1.remove("LONG");
                e1.commit();
            }
        }
        else if(status==1){
            Toast.makeText(getApplicationContext(),"TEMPORARILY_UNAVAILABLE",Toast.LENGTH_SHORT).show();            
        }else if(status==2){
            Toast.makeText(getApplicationContext(),"AVAILABLE",Toast.LENGTH_SHORT).show();
        }    
    }   

    }/* End of Class MyLocationListener */enter code here

`
4

1 に答える 1

1

このようなことを試してください

public class ShowLocationActivity extends Activity implements LocationListener {
      private TextView latituteField;
      private TextView longitudeField;
      private LocationManager locationManager;
      private String provider;


    /** Called when the activity is first created. */

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        latituteField = (TextView) findViewById(R.id.TextView02);
        longitudeField = (TextView) findViewById(R.id.TextView04);

        // Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use
        // default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

        // Initialize the location fields
        if (location != null) {
          System.out.println("Provider " + provider + " has been selected.");
          onLocationChanged(location);
        } else {
          latituteField.setText("Location not available");
          longitudeField.setText("Location not available");
        }
      }

      /* Request updates at startup */
      @Override
      protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
      }

      /* Remove the locationlistener updates when Activity is paused */
      @Override
      protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
      }

      @Override
      public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude());
        int lng = (int) (location.getLongitude());
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
      }

      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

      }

      @Override
      public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

      }

      @Override
      public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
      }
    } 

このチュートリアルを参照してください

于 2013-04-23T04:58:51.780 に答える