放大标记谷歌地图V2

时间:2014-05-30 19:29:58

标签: java android google-maps

我有一个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

我怎样才能让它发挥作用?

1 个答案:

答案 0 :(得分:64)

问题是地图尚未加载到布局,这就是你遇到错误的原因

<强>解决方案:

 map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
    @Override
    public void onMapLoaded() {
        map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 15));
        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    }
});

当地图已加载到布局时,它将调用该方法。