android - 在android中获取GPS坐标

标签 android

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

点击按钮我刚刚调用了 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

    }

}

任何人都可以帮忙吗?这里有什么问题???

最佳答案

这个对我有用它还有一些其他传感器 你还需要在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 

关于android - 在android中获取GPS坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5772248/

相关文章:

android - 在 twitter4j 中固定

android - 随时监听关键字,例如 4.4 上的 "Ok google"

android - 每次执行docker run ./gradlew命令时,它都会下载所有gradle文件和依赖项。如何仅下载和安装一次?

android - findViewById 与 setId

java - Android 手机不转储堆栈跟踪

java - 在 Android 中从 JSON 文件生成菜单项

android - RecyclerView 什么都不显示

android - 我应该如何以编程方式打开三星设备上的飞行模式设置页面?

java - 是3DES = DES 做3次吗?

android - 底部栏顶部的 float 操作按钮