我想在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中,因为我根本没有到达我的祝酒词。
UDATE:
我尝试将getter添加到MyLocationListenerClass:
public double getLatitude(){
return latitude;
}
public double getLongitude(){
return longitude;
}
但是当我回到我的活动时,我会尝试这一切:
double latitude = mlocListener.getLatitude();
但是我在getLatitdue()
上得到了一个无法解决的方法错误答案 0 :(得分:0)
我认为这是问题所在:
你正在调用Class(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);
}
这应该有效