gpt4 book ai didi

android - 如何使纬度和经度全局变量?

转载 作者:行者123 更新时间:2023-11-29 14:54:20 27 4
gpt4 key购买 nike

我已经将纬度和经度设为全局变量。

它在 MyLocationListener 类中工作正常,因为它显示当前位置的纬度和经度,但是当它加载 url ( http://maps.google.com/maps?"+ "saddr="
+ 当前纬度 + ","+ 当前经度
+ "&daddr=27.11,85.11")
) 在 webview 中,它加载值 0 和 0。

为什么?

public class ShowMapActivity extends Activity {

private WebView webView;
double currentLattitude;
double currentLongtitude;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);

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

webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://maps.google.com/maps?" + "saddr="
+ currentLattitude + "," + currentLongtitude
+ "&daddr=27.11,85.11");

}

public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
currentLattitude = loc.getLatitude();

currentLongtitude = loc.getLongitude();

Toast.makeText(
getApplicationContext(),

"Latitude is: " + currentLattitude + " Longitude is: "
+ currentLongtitude,

Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderDisabled(String provider)
{

Toast.makeText(getApplicationContext(),

"Gps Disabled",

Toast.LENGTH_SHORT).show();

}

@Override
public void onProviderEnabled(String provider)
{

Toast.makeText(getApplicationContext(), "Gps Enabled",

Toast.LENGTH_SHORT).show();

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{

}
}
}

最佳答案

尝试把这段代码放在onLocationChanged

webView = (WebView) findViewById(R.id.webView1); 
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://maps.google.com/maps?" + "saddr="
+ currentLattitude + "," + currentLongtitude
+ "&daddr=27.11,85.11");

关于android - 如何使纬度和经度全局变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9973540/

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