我要做的就是从最后知道的位置返回long / lat值,如果为null,则拉出新的coords。
我对此很新,这就是我所拥有的:
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class GPS extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
TextView output;
output = (TextView) findViewById(R.id.output);
output.append("\n\nLocations (starting with last known):");
Location location = locationManager.getLastKnownLocation(bestProvider);
printLocation(location);
}
}
答案 0 :(得分:1)
检查位置,如果它们为null,则必须使用 locationManager.requestLocationUpdates(...)函数。
我认为你还必须实现一个位置监听器,以便在现场条件下有效地工作。
我已经回答了here
答案 1 :(得分:1)
不要忘记在AndoidManifest.xml中添加ACCESS_COARSE_LOCATION
或ACCESS_FINE_LOCATION
权限。