【问题标题】:GPS location never aquiredGPS 位置从未获得
【发布时间】:2013-12-07 00:14:44
【问题描述】:

我正在尝试在 android 中获取 GPS 位置,并使用 gps 坐标运行 ASYNC 任务。我的位置类不仅仅是在打开 gps 的情况下停止应用程序。我没有得到 GPS 坐标。

我试图获取 gps 位置的活动是:

public class FindBrewery extends ActionbarMenu {


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);




        setContentView(R.layout.beer_location_list);

        String title = "Nearby Breweries";
        TextView topTitle = (TextView) findViewById(R.id.beerLocationTitle);
        topTitle.setText(title);

        //get user location

        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);


        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        ///new code
        LocationManager mlocManager=null;
        LocationListener mlocListener;
        mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

        if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            if(MyLocationListener.latitude>0)
            {

                double longitude = MyLocationListener.latitude;
                double latitude = MyLocationListener.longitude;

                Toast.makeText(getApplicationContext(), "inside gps if", Toast.LENGTH_LONG).show();

                //get gps results
                //construct url
                String url = "myURL";

                Log.d("urlTest",url);

                //async task goes here
                new GetNearbyBreweries(this).execute(url);


            }
            else
            {
                //not sure what to put here yet...
            }

        } else {


            Toast.makeText(getApplicationContext(), "GPS is Not Turned on", Toast.LENGTH_LONG).show();
        }

    }


        //new code end



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main2, menu);

        return true;
    }

}

MyLocationListener 类:

package com.example.beerportfoliopro;

/**
 * Created by Mike on 11/26/13.
 */
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;

public class MyLocationListener implements LocationListener {

    public static double latitude;
    public static double longitude;

    @Override
    public void onLocationChanged(Location loc)
    {
        loc.getLatitude();
        loc.getLongitude();
        latitude=loc.getLatitude();
        longitude=loc.getLongitude();
    }

    @Override
    public void onProviderDisabled(String provider)
    {
        //print "Currently GPS is Disabled";
    }
    @Override
    public void onProviderEnabled(String provider)
    {
        //print "GPS got Enabled";
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
    }
}

我知道它卡在 MyLocationListenerClass 中,因为我根本没有吃到我的吐司。

日期:

我尝试将 getter 添加到 MyLocationListenerClass:

public double getLatitude(){

        return latitude;

    }

    public double getLongitude(){

        return longitude;

    }

但是当我回到调用这一切的活动时,我会尝试:

double latitude = mlocListener.getLatitude();

我在 getLatitdue() 上遇到无法解析方法错误

【问题讨论】:

  • 你会得到任何异常,错误与否?
  • Manifest 中有哪些权限?
  • 你怎么知道你没有得到任何回报?
  • 在你的onLocationChanged:Log.d("new Location", "latitude: "+ loc.getLatitude()+ ", longitude: "+loc.getLongitude());中执行此操作

标签: android gps


【解决方案1】:

我认为这是问题所在:

您正在调用类而不是 (MyLocationListener) 调用类 mlocListener 的实例

if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            if(MyLocationListener.latitude>0)
            {

                double longitude = MyLocationListener.latitude;
                double latitude = MyLocationListener.longitude;

                Toast.makeText(getApplicationContext(), "inside gps if", Toast.LENGTH_LONG).show();

                //get gps results
                //construct url
                String url = "myURL";

                Log.d("urlTest",url);

                //async task goes here
                new GetNearbyBreweries(this).execute(url);


            }

尝试这样做:

if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            if(mlocListener.latitude>0)
            {

                double longitude = mlocListener.latitude;
                double latitude = mlocListener.longitude;

                Toast.makeText(getApplicationContext(), "inside gps if", Toast.LENGTH_LONG).show();

                //get gps results
                //construct url
                String url = "myURL";

                Log.d("urlTest",url);

                //async task goes here
                new GetNearbyBreweries(this).execute(url);


            }

这应该可以工作

【讨论】:

  • 红色波浪线下划线经纬度并表示,无法解析符号
  • 你能不能在MyLocationListener类中初始化经纬度并将它们设置为0。也取出loc.getLatitude(); loc.getLongitude();从 onLocationChange()
  • 试过了,同样的事情没有发生。
  • 你去掉了静电吗?您可能需要一个 getter 方法来获取值。
  • 我把静态关掉了,它不能编译:Gradle: non-static variable latitude cannot be referenced from a static context
猜你喜欢
  • 2011-05-04
  • 2015-05-17
  • 1970-01-01
  • 2021-12-06
  • 1970-01-01
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多