【发布时间】: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());中执行此操作