gpt4 book ai didi

android - 帮助使用 GPS 坐标,Android

转载 作者:太空狗 更新时间:2023-10-29 13:41:21 25 4
gpt4 key购买 nike

我正在使用此代码获取我的位置并将坐标打印在屏幕上。

package com.example.alpha;  

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class alpha extends Activity
{
private LocationManager lm;
private LocationListener locationListener;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//---use the LocationManager class to obtain GPS locations---
lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);

locationListener = new MyLocationListener();

lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
locationListener);
}

public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc) {


double lat = loc.getLatitude();
double lon = loc.getLongitude();
Toast.makeText(getBaseContext(), "Location changed : alphaLat: " + lat +
" alphaLng: " + lon,
Toast.LENGTH_SHORT).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
}

}

}

问题是我想使用纬度和经度值,以便通过流发送它们。关于如何在 onLocationChanged 方法之外获取坐标的任何建议?

最佳答案

不是将 lat 和 lon 声明为局部变量,而是将它们声明为类级别变量。这样您就可以在 onLocationChanged 方法之外访问这些值。

public class alpha extends Activity
{
private LocationManager lm;
private LocationListener locationListener;
double lat;
double lon;

public class MyLocationListener implements LocationListener
{
...

lat = loc.getLatitude();
lon = loc.getLongitude();

........

关于android - 帮助使用 GPS 坐标,Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5384389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com