我使用地图,最初如何设置缩放比例(大约只能看到这些标记所在的城市)?
我的 MapActivity 类:
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
private ChildEventListener mChildEventListener;
private DatabaseReference mFruitRef = FirebaseDatabase.getInstance().getReference("Fruit");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
createMapView();
}
private void createMapView() {
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setOnMarkerClickListener(marker -> false);
googleMap.getUiSettings().setZoomControlsEnabled(true);
Marker marker = new Marker("Orange", 47.000327, 28.867950, "some info");
Marker marker2 = new Marker("Bananas", 47.045029, 28.861427, "some info");
Marker marker3 = new Marker("Peaches", 47.024204, 28.889236, "some info");
mFruitRef.push().setValue(marker);
mFruitRef.push().setValue(marker2);
mFruitRef.push().setValue(marker3);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//get marker info from Firebase Database and add to map
addMarkersToMap(googleMap);
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
googleMap.setMyLocationEnabled(true);
}
@Override
public void onStop() {
if (mChildEventListener != null)
mFruitRef.removeEventListener(mChildEventListener);
super.onStop();
}
private void addMarkersToMap(GoogleMap map) {
mChildEventListener = mFruitRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Marker marker = dataSnapshot.getValue(Marker.class);
String name = marker.getName();
String description = marker.getDescription();
double latitude = marker.getLatitude();
double longitude = marker.getLongitude();
LatLng location = new LatLng(latitude, longitude);
map.addMarker(new MarkerOptions().title(name).snippet(description).position(location));
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
您可以根据标记设置缩放:
事实上,你可以只
onChildAdded()
输入你的方法:并根据所需的缩放比例,使用第二个参数。
那么,另外,作为一个选项,你可以试试这个方法: