我有一个自定义对象的ArrayList
,其中包含lat和long属性。
我已经成功在地图上标记了这些标记。我想要的是在地图加载时能够缩放到这些标记。
我尝试了下面的代码,但导致应用程序崩溃。
以下是加载地图的代码:
public void getBridgeData() {
db = new DbHelperClass(this);
bridges = db.getAllBridges();
DecimalFormat dc = new DecimalFormat("#.000");
Builder builder = new LatLngBounds.Builder();
for (int i= 0;i<bridges.size();i++) {
Bridge b= (Bridge)bridges.get(i);
double lati= b.getLatitude();
double longo= b.getLongitude();
double reflat= b.getReflat();
double reflong= b.getReflong();
LatLng start = new LatLng(reflat,reflong);
LatLng end = new LatLng(lati,longo);
GeneralUtils uts = new GeneralUtils();
double ch= uts.calculateDistance(start, end);
String chainage = dc.format(ch);
String mysnipet = "Location:" + b.getRoadname() + " " + chainage;
Marker mm=map.addMarker(new
MarkerOptions().position(new LatLng(lati, longo))
.title(b.getBridgename())
.snippet(mysnipet));
builder.include(mm.getPosition());
}
LatLngBounds bounds= builder.build();
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 15));
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
这里还有一部分代码
public class MainActivity extends FragmentActivity {
private GoogleMap map;
private DbHelperClass db;
private List<Bridge> bridges = new ArrayList<Bridge>();
CameraUpdate cu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_map);
//AddSomeData();
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
if(map!=null) {
getBridgeData();
}
}
}
这是错误日志:
05-30 22:19:28.894: E/AndroidRuntime(5593): FATAL EXCEPTION: main
05-30 22:19:28.894: E/AndroidRuntime(5593): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bridge.bridgeinventory/com.bridge.bridgeinventory.MainActivity}: java.lang.IllegalStateException: Error using newLatLngBounds(LatLngBounds, int): Map size can't be 0. Most likely, layout has not yet occured for the map view. Either wait until layout has occurred or use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map's dimensions.
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1755)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1774)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.access$1500(ActivityThread.java:157)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1001)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.os.Handler.dispatchMessage(Handler.java:130)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.os.Looper.loop(SourceFile:351)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.main(ActivityThread.java:3841)
05-30 22:19:28.894: E/AndroidRuntime(5593): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 22:19:28.894: E/AndroidRuntime(5593): at java.lang.reflect.Method.invoke(Method.java:538)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
05-30 22:19:28.894: E/AndroidRuntime(5593): at dalvik.system.NativeStart.main(Native Method)
05-30 22:19:28.894: E/AndroidRuntime(5593): Caused by: java.lang.IllegalStateException: Error using newLatLngBounds(LatLngBounds, int): Map size can't be 0. Most likely, layout has not yet occured for the map view. Either wait until layout has occurred or use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map's dimensions.
05-30 22:19:28.894: E/AndroidRuntime(5593): at kbh.b(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at mas.a(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at mal.a(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at mbi.a(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at fms.onTransact(SourceFile:83)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.os.Binder.transact(Binder.java:310)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.moveCamera(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.bridge.bridgeinventory.MainActivity.getBridgeData(MainActivity.java:148)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.bridge.bridgeinventory.MainActivity.onCreate(MainActivity.java:44)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1719)
05-30 22:19:28.894: E/AndroidRuntime(5593): ... 11 more
我该如何让它工作?