gpt4 book ai didi

java - 单击按钮时获取当前位置

转载 作者:行者123 更新时间:2023-12-01 09:34:35 25 4
gpt4 key购买 nike

我有一个谷歌地图应用程序,现在我添加了一个按钮。当我单击按钮时,我想收到一 strip 有我的坐标的消息(Toast)。哪种方法最简单?我还必须实现 onLocationChanged 类吗?

最佳答案

您可以使用不同的方式来实现它。我倾向于认为最适合您的任务是在按下我的位置按钮后显示 toast。

public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;

/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

//Show my location button
mMap.setMyLocationEnabled(true);

mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
//Do something with your location. You can use mMap.getMyLocation();
//anywhere in this class to get user location
Toast.makeText(MapActivity.this, String.format("%f : %f",
mMap.getMyLocation().getLatitude(), mMap.getMyLocation().getLongitude()),
Toast.LENGTH_SHORT).show();
return false;
}
});
}
}

此外,您还可以在用户位置每次更改时显示 toast。为此,请设置 OnMyLocationChangeListener

    mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(android.location.Location location) {
//...
}
});

关于java - 单击按钮时获取当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39108776/

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