在android中获取Gps坐标

时间:2011-04-24 17:59:07

标签: android

我无法以编程方式获取gps。堆栈溢出给出了很多例子,但我仍然得到空值。我想做什么,我有一个按钮,点击按钮,我想获得gps坐标。这是我的代码,以获得gps cordinate。

按钮点击我刚刚调用了getlocation()

private LocationManager lm;
private LocationListener locationListener;

public void getlocation()
{       

    lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
    locationListener = new MyLocationListener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

}

 class MyLocationListener implements LocationListener 
 {
     String a;
     String b;
     String c;

    public void onLocationChanged(Location loc) 
    {
        if (loc != null) {

            LAT = loc.getLatitude();
            LONG = loc.getLongitude();  
            a=Double.toString(LAT);
            b=Double.toString(LONG);
            c="-----LAT is:"+a+"  "+"LONG is--------:"+b;
            System.out.println(c);
            //Toast.makeText(context, "Your location is:"+c, Toast.LENGTH_LONG).show();




        }
    }

    @Override
    public void onProviderDisabled(String provider) 
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider)
    {
        // TODO Auto-generated method stub

    }

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

    }

}

任何身体都能帮忙吗?这里有什么不对???

1 个答案:

答案 0 :(得分:1)

这个对我有用,它还有一些其他传感器 您还需要在AndroidManifest文件中配置前提

 

package at.fhb.g.sensor;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import at.fhb.g.main.MainControler;

/**
 * @author tomas
 *the sensor class calls the onsensorchanged method on every item in the onsensorchangedlistener list when a sensor change happens
 */

public class Sensor implements SensorListener, LocationListener {
    // commit comment

    private SensorManager sensorManager;
    private final MainControler context;
    private float accelx;
    private float accely;
    private float accelz;
    private float rotY;
    private float rotX;
    private float rotZ;
    private List sensorsChangedListeners=new ArrayList();
    private LocationManager locationManager;
    private Location lastKnownLocation;
//  private float kRotX;
    private float kRotY;
    private float kRotZ;//TODO remove kalibrate junk
    private CFilter filter;
//  private float[] rotBuffY;
//  private float[] rotBuffZ;
//  private float[] rotBuffX;
    private float theG;


private void initGps() {

        locationManager=(LocationManager) context.getSystemService(Activity.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, context.getConfig().getGpsUpdateTime(), context.getConfig().getGpsMinDistance(), this) ;
        lastKnownLocation=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    }
    public Sensor(MainControler context) {
        this.context = context;
        filter=new CFilter(context);
//      rotBuffX=new float[context.getConfig().getRotBufferSize()];
//      rotBuffZ=new float[context.getConfig().getRotBufferSize()];TODO remove this kalibrate junk
//      rotBuffY=new float[context.getConfig().getRotBufferSize()];
        sensorManager = (SensorManager) context.getSystemService(Activity.SENSOR_SERVICE);
        onResume();
        initGps();
    }



    /**
     * @param listener ads a listener to the lists
     */
    public void addOnSensorChangeListener(SensorsChangedListener listener) {

        sensorsChangedListeners.add(listener);

    }


         /**
         * this method needs to be called in the activity onResume function
         */
        public void onResume() {

            // register this class as a listener for the orientation and accelerometer sensors
            sensorManager.registerListener(this, 
                    SensorManager.SENSOR_ORIENTATION |SensorManager.SENSOR_ACCELEROMETER,
                    SensorManager.SENSOR_DELAY_FASTEST);
            System.out.println("on resume");
         }
         /**
         * this method needs to be called in the activity onStop function
         */
        public void onStop() {
             // unregister listener
             sensorManager.unregisterListener(this);

         }
        public void onAccuracyChanged(int sensor, int accuracy) {
            // TODO Auto-generated method stub

        }
        public void onSensorChanged(int sensor, float[] values) {

            synchronized(this){
          //TODO G 
                float g = (float) Math.sqrt( accelx* accelx+ accely* accely+ accelz* accelz );
          theG=filter.filter(g);
           //filter TODO replace accelx wit theG
//              rotY=filter.filter(rotY);

                if (sensor == SensorManager.SENSOR_ORIENTATION) {

                    rotX= values[0];
                 rotY= values[1];
                    rotZ= values[2];
                }
                if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
                    accelx=values[0];
                   accely= values[1];
                    accelz= values[2];


                }

                //shiftbuffer
//              shiftArrayLeft(rotBuffX);
//              shiftArrayLeft(rotBuffY);
//              shiftArrayLeft(rotBuffZ);
//              
//              //save to buffer
//              rotBuffX[rotBuffX.length-1]=rotX;
//              rotBuffY[rotBuffY.length-1]=rotY;
//              rotBuffZ[rotBuffZ.length-1]=rotZ;

                notifySensorChanged();
        }
        }

/**
 * @return current g force
 */
public float getTheG() {
    return theG;
}

//
//      private void shiftArrayLeft(float[] a) {
//      for (int i = 0; i 
相关问题