gpt4 book ai didi

java - 获取 Google map 的当前位置

转载 作者:行者123 更新时间:2023-12-01 14:57:06 25 4
gpt4 key购买 nike

Possible Duplicate:
What is the simplest and most robust way to get the user’s current location in Android?

我有以下代码,可以在手机上打开 Google map ,并将目的地的经度 + 纬度和起始位置传递给它。我想知道是否有一种方法可以让我们不必在代码中手动输入起始位置,而是以某种方式让代码自动找出用户所在的位置?

add.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent (android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=" + 51.5171 +
"," + 0.1062 + "&daddr=" + 52.6342 + "," + 1.1385));

intent.setComponent(
new ComponentName ("com.google.android.apps.maps",
"com.google.android.maps.MapsActivity"));

startActivity(intent);
}
});

最佳答案

您可以使用此方法:

public LatLng getLocation(Context ctx) 
{
LocationManager lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);

/*
* Loop over the array backwards, and if you get an accurate location,
* then break out the loop
*/
Location l = null;

for (int i = providers.size() - 1; i >= 0; i--)
{
l = lm.getLastKnownLocation(providers.get(i));
if (l != null)
break;
}
return new LatLng(l.getLatitude(),l.getLongitude());
}

关于java - 获取 Google map 的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14223831/

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