我正在android中创建一个MapFragment;地图显示正常(虽然打开需要10秒),但没有显示标记。
这是片段的代码:
public class VehiclesMapFragment extends MapFragment implements OnMapReadyCallback {
GoogleMap _map;
private void setUpMapIfNeeded() {
if (_map == null) {
_map = this.getMap();
//_map = this.getMap();
//if (_map != null) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
ReDrawList(_vehicles);
}
}, 2000);
//}
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
_map = googleMap;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
ReDrawList(_vehicles);
}
}, 10000);
}
public VehiclesMapFragment() {
}
VehiclesPosition[] _vehicles = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_vehicles = (VehiclesPosition[])getArguments().getSerializable("VEHICLES");
//setUpMapIfNeeded();
}
public void ReDrawList(VehiclesPosition[] vehicles)
{
if (vehicles != null && vehicles.length > 0 && _map != null) {
LatLngBounds bounds = null;
for (VehiclesPosition vp : vehicles) {
LatLng latlngPos = vp.GetMapLatLng();
if (bounds == null)
bounds = new LatLngBounds(latlngPos, latlngPos);
else
bounds = bounds.including(latlngPos);
MarkerOptions mi = vp.GetMapMarker();
Marker mm = _map.addMarker(mi);
mm.showInfoWindow();
}
try{
_map.animateCamera(CameraUpdateFactory.zoomTo(12));
_map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds,100));
}catch (Exception exx) {
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_vehicles_map, container, false);
setUpMapIfNeeded();
return v;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
//setUpMapIfNeeded();
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void onResume() {
super.onResume();
}
private OnFragmentInteractionListener mListener;
public interface OnFragmentInteractionListener {
public void onFragmentInteraction(Uri uri);
}
}
这是布局文件:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
并且片段由活动实例化,其中包含:
Bundle args = new Bundle();
args.putSerializable("VEHICLES",_VehiclesList);
fragment_units_map = new VehiclesMapFragment();
_fragment_units_map.setArguments(args);
调用_map.animateCamera会在每次调用时产生异常: java.lang.IllegalStateException:使用newLatLngBounds时出错(LatLngBounds,int):地图大小不能为0.很可能,地图视图尚未出现布局。要么等到布局发生,要么使用newLatLngBounds(LatLngBounds,int,int,int),它允许你指定地图的尺寸。