Googlemaps android设置界限

时间:2015-03-13 11:14:58

标签: android

我可以在网上找到的大多数示例使用现已弃用的getMap()方法。 Google表示使用getMapAsync()。我可以在地图上绘制标记但是当我包含边界代码时,我得到一个IllegalStateException。那么,我的问题是如何正确设置LatLngBounds.Builder?

代码和LogCat如下:

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;
}

}

logcat的:

FATAL EXCEPTION: main
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.
        at com.google.k.a.cj.b(Unknown Source)
        at com.google.maps.api.android.lib6.gmm6.c.c.a(Unknown Source)
        at com.google.maps.api.android.lib6.c.dp.a(Unknown Source)
        at com.google.maps.api.android.lib6.gmm6.c.c.a(Unknown Source)
        at com.google.maps.api.android.lib6.c.el.a(Unknown Source)
        at com.google.android.gms.maps.internal.l.onTransact(SourceFile:83)
        at android.os.Binder.transact(Binder.java:326)
        at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.moveCamera(Unknown Source)
        at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
        at mtr.se.reskoll.TripsMap.onMapReady(TripsMap.java:87)
        at com.google.android.gms.maps.MapFragment$a$1.a(Unknown Source)
        at com.google.android.gms.maps.internal.m$a.onTransact(Unknown Source)
        at android.os.Binder.transact(Binder.java:326)
        at com.google.android.gms.maps.internal.ba.a(SourceFile:82)
        at com.google.maps.api.android.lib6.c.er.run(Unknown Source)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4898)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

使用此

SupportMapFragment mapFragment =(SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

而不是

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);

问题还可能是由于文档中提到here的原因。

  

在地图出现之前,请勿使用此相机更新更换相机   经历布局(为了使这种方法正确地确定   适当的边界框和缩放级别,地图必须有一个大小)。   否则将抛出IllegalStateException。它不是   足以使地图可用(即getMap()返回一个   非空对象);包含地图的视图也必须经历过   布局使其尺寸已确定。如果你不能   确定已发生这种情况,请使用newLatLngBounds(LatLngBounds,int,   改为int,int)并手动提供地图的尺寸​​。