我有一个谷歌地图应用程序,现在我添加了一个按钮。当我点击按钮时,我想用我的坐标得到一条消息(Toast)。这是最简单的方法吗?我是否还必须实现onLocationChanged类?
答案 0 :(得分:1)
您可以使用不同的方式来实现它。我倾向于认为最适合你的任务是在按下我的位置按钮后显示吐司。
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;
}
});
}
}
每次更改时,您都可以使用用户位置显示烤面包。为此,设置OnMyLocationChangeListener
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(android.location.Location location) {
//...
}
});
答案 1 :(得分:0)
答案 2 :(得分:0)