谷歌地图android设置边界
我可以在网上找到的大多数示例都使用现在不推荐使用的getMap()方法。谷歌说使用getMapAsync()。我可以在地图上画标记,但是当我包含边界代码时,我会得到一个非法状态异常。那么,我的问题是如何正确设置LatLngBounds.Builder 代码和日志如下:谷歌地图android设置边界,android,Android,我可以在网上找到的大多数示例都使用现在不推荐使用的getMap()方法。谷歌说使用getMapAsync()。我可以在地图上画标记,但是当我包含边界代码时,我会得到一个非法状态异常。那么,我的问题是如何正确设置LatLngBounds.Builder 代码和日志如下: public class TripsMap extends FragmentActivity implements OnMapReadyCallback { MapView mMapView; private GoogleMap
public class TripsMap extends FragmentActivity implements OnMapReadyCallback {
MapView mMapView;
private GoogleMap googleMap;
GoogleMapOptions options = new GoogleMapOptions();
private LatLngBounds.Builder bounds;
Trips trip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trips_map_fragment);
Bundle extras = getIntent().getExtras();
trip = (Trips) extras.getSerializable("tripInfo");
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
bounds = new LatLngBounds.Builder();
int subTrips = trip.getSubtripsNum();
for (int i=0; i<subTrips; i++) {
SubTrips st = trip.getSubTrips().get(i);
//major origin
googleMap.addMarker(new MarkerOptions()
.position(convertLatLng(st.originCoords))
.title(st.origin));
Log.i("MAJOR", st.origin);
//use .include to put add each point to be included in the bounds
bounds.include(convertLatLng(st.originCoords));
int minor = st.intermediateStation.size();
for (int j=0; j<minor; j++){
IntermediateStops is = st.intermediateStation.get(j);
//minor stop
googleMap.addMarker(new MarkerOptions()
.position(convertLatLng(is.coords))
.title(is.stationName));
}
//major destination
googleMap.addMarker(new MarkerOptions()
.position(convertLatLng(st.destCoords))
.title(st.destination));
bounds.include(convertLatLng(st.destCoords));
}
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), 50));
}
public LatLng convertLatLng(String s) {
String[] latlong = s.split(",");
double latitude = Double.parseDouble(latlong[0]);
double longitude = Double.parseDouble(latlong[1]);
LatLng ll = new LatLng(latitude, longitude);
return ll;
}
用这个
SupportMapFragment mapFragment =(SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
而不是
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
问题也可能是由于文档中提到的原因造成的
在地图显示之前,请勿使用此相机更新更改相机
进行布局(以使此方法正确确定
适当的边界框和缩放级别,贴图必须具有大小)。
否则将抛出非法状态异常。事实并非如此
足以使映射可用(即getMap()返回
非空对象);包含该地图的视图也必须经过
已确定其尺寸的布局。如果你不能
确保已发生这种情况,请使用newLatLngBounds(LatLngBounds,int,
int,int)并手动提供地图的尺寸
当我将一个片段的宽度和高度设置为匹配父对象并包装内容时,如何将其硬编码为像素?
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);