地图活动未显示地图

时间:2016-01-06 08:54:32

标签: java android google-maps google-maps-markers

我想以两种方式在不同时间进行两次地图活动 01.在地图上显示两个位置(假设当前用户的位置和结束位置)。 02.在地图上显示几个位置(用户的当前位置和几个选定位置)。在这两种情况下,当使用第二种方式时,使用intent.it包括字符串数组从另一个活动中获取位置。 我怎么两次都有错误说

" java.lang.IllegalStateException:使用newLatLngBounds(LatLngBounds,int)时出错:地图大小不能为0.很可能,地图视图尚未出现布局。要么等到布局发生,要么使用newLatLngBounds(LatLngBounds,int,int,int),它允许你指定地图的尺寸​​。"

我曾多次尝试解决这个问题,但我无法解决。如果有人可以帮助我,那将对我的最后一年项目产生巨大的影响。我的编码如下。

第一种方法的活动

package com.ksfr.sdfinder;

public class DistanceFinderMainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

//codes
public void FindDistanceButtonClicked (View view){
    Data_Select();
    if (selectedview=="text"){
        distance_cur_to_end_DF=cur_Location.distanceTo(end_Location)/1000;
        DecimalFormat df = new DecimalFormat("#.###");
        df.setMinimumFractionDigits(3);
        Str_distance=String.valueOf(df.format(distance_cur_to_end_DF));
        Intent DFTxtIntent =new Intent(DistanceFinderMainActivity.this,DistanceFinderTextActivity.class);
        DFTxtIntent.putExtra("provider",Provider);
        DFTxtIntent.putExtra("accuracy",Str_accuracy);
        DFTxtIntent.putExtra("schoolName",Str_SchName);
        DFTxtIntent.putExtra("schoolAddress",Str_SchAddress);
        DFTxtIntent.putExtra("schoolType",StrType);
        DFTxtIntent.putExtra("DFDistance",Str_distance);

        startActivity(DFTxtIntent);
    }
    if (selectedview=="map"){
        Intent DFMapIntent = new Intent(DistanceFinderMainActivity.this,MapsActivity.class);
        DFMapIntent.putExtra("source",2);
        DFMapIntent.putExtra("DFcur_latitude",cur_latitude);
        DFMapIntent.putExtra("DFcur_longitude",cur_longitude);
        DFMapIntent.putExtra("DFend_latitude",end_latitude);
        DFMapIntent.putExtra("DFend_longitude",end_longitude);
        DFMapIntent.putExtra("schoolName",Str_SchName);

        startActivity(DFMapIntent);
    }
}
}

第二种方法的活动

public class SchoolSelectorMainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
//codes

if(selectedview=="map"){
        if (listCount!=0){
            Intent SSMapIntent =new Intent(SchoolSelectorMainActivity.this,MapsActivity.class);
            SSMapIntent.putExtra("maxDistance",Str_maxdistance);
            SSMapIntent.putExtra("source", 1);
            SSMapIntent.putExtra("listCount", listCount);
            if(listCount!=0){
                SSMapIntent.putExtra("nameList",nameListIntent);
                SSMapIntent.putExtra("typeList", typeListIntent);
                SSMapIntent.putExtra("distanceList",distanceListIntent);
                SSMapIntent.putExtra("latitude",latListIntent);
                SSMapIntent.putExtra("longitude", lngListIntent);
                SSMapIntent.putExtra("cur_lat",cur_latitude);
                SSMapIntent.putExtra("cur_lng", cur_longitude);
            }
            startActivity(SSMapIntent);
       }
    }
}

我的mapactivity

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
//codes
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    Intent intent = this.getIntent();
    Bundle bundle = intent.getExtras();
    source=bundle.getInt("source");
    maxDistance=bundle.getDouble("maxDistance");
    if (source==1){
        listCount=bundle.getInt("listCount");
        cur_lat=bundle.getDouble("cur_lat");
        cur_lng=bundle.getDouble("cur_lng");

        if (listCount!=0) {
            String[] nameListMap = bundle.getStringArray("nameList");
            String[] typeListMap = bundle.getStringArray("typeList");
            String[] distanceListMap= bundle.getStringArray("distanceList");
            double [] latListMap=bundle.getDoubleArray("latitude");
            double [] lngListMap=bundle.getDoubleArray("longitude");

            try{
                for (int l =0;l<listCount;l++)
                {
                    selectedListMap[l][0]=nameListMap[l];
                    selectedListMap[l][1]=typeListMap[l];
                    selectedListMap[l][2]=distanceListMap[l];
                    selectedListMap[l][3]=String.valueOf(latListMap[l]);
                    selectedListMap[l][4]=String.valueOf(lngListMap[l]);
                }
            }catch (NullPointerException e){
                Toast.makeText(MapsActivity.this, "Null pointer catched",
                        Toast.LENGTH_LONG).show();
            }
        }
               }
    if (source==2){
        cur_lat=bundle.getDouble("DFcur_latitude");
        cur_lng=bundle.getDouble("DFcur_longitude");
        cur_latLngDF =new LatLng(cur_lat,cur_lng);
        end_lat=bundle.getDouble("DFend_latitude");
        end_lng=bundle.getDouble("DFend_longitude");
        end_latLngDF=new LatLng(end_lat,end_lng);
        schName=bundle.getString("schoolName");
    }
}
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    Marker[] end_marker=new Marker[listCount];
    LatLng[] end_latLng=new LatLng[listCount];

    switch (source){
        case 1:
            for (int l =0;l<listCount;l++)
            {
                end_latLng[l]=new LatLng(Double.parseDouble(selectedListMap[l][3]) ,Double.parseDouble(selectedListMap[l][4]));
                end_marker[l] = mMap.addMarker(new MarkerOptions()
                        .position(end_latLng[l])
                        .title(selectedListMap[l][0])
                        .snippet("Distance :" + selectedListMap[l][2] + " km."));

            }
            cur_latLngSS=new LatLng(cur_lat,cur_lng);

            cur_Marker = mMap.addMarker(new MarkerOptions()
                    .position(cur_latLngSS)
                    .title("User Location")
                    .snippet("User's Location Here."+cur_latLngSS)
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));

            for (int l=0;l<listCount;l++){
                builder1.include(end_latLng[l]);
                end_marker[l].showInfoWindow();
            }
            builder1.include(cur_latLngSS);
            cur_Marker.showInfoWindow();
            LatLngBounds bounds1 = builder1.build();
            mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds1, 100));
            break;
        case 2:
            builder1.include(cur_latLngDF);
            builder1.include(end_latLngDF);
            cur_MarkerDF = mMap.addMarker(new MarkerOptions()
                    .position(cur_latLngDF)
                    .title("User Location")
                    .snippet("User's Location Here."+cur_latLngDF)
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
            end_MarkerDF = mMap.addMarker(new MarkerOptions()
                    .position(end_latLngDF)
                    .title(schName)
                    .snippet("Distance from user's location:"+distance+"km")
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
            cur_MarkerDF.showInfoWindow();
            end_MarkerDF.showInfoWindow();
            LatLngBounds bounds2 = builder1.build();
            mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds2, 20));
            break;

    }


}

错误日志

01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime: FATAL EXCEPTION: main
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime: Process: com.ksfr.sdfinder, PID: 7720
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime: 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.
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at com.google.a.a.ae.b(Unknown Source)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at com.google.maps.api.android.lib6.gmm6.c.a.a(Unknown Source)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at com.google.maps.api.android.lib6.e.dy.a(Unknown Source)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at com.google.maps.api.android.lib6.gmm6.c.a.a(Unknown Source)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at com.google.maps.api.android.lib6.e.ev.a(Unknown Source)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at com.google.android.gms.maps.internal.j.onTransact(SourceFile:83)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at android.os.Binder.transact(Binder.java:361)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.moveCamera(Unknown Source)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at com.ksfr.sdfinder.MapsActivity.onMapReady(MapsActivity.java:166)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at com.google.android.gms.maps.internal.zzl$zza.onTransact(Unknown Source)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at android.os.Binder.transact(Binder.java:361)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at com.google.android.gms.maps.internal.be.a(SourceFile:82)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at com.google.maps.api.android.lib6.e.fb.run(Unknown Source)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:808)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:103)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5333)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
01-06 14:20:37.922 7720-7720/com.ksfr.sdfinder E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我的编码中的问题是相机更新方法。我使用后续编码并且它对我有效。

private static final double PADDING_PERCENTAGE = 0.12;// offset from edges of the map 12% of screen
public void onMapReady(GoogleMap googleMap) {
    //codes
    int width = getResources().getDisplayMetrics().widthPixels;
    int height = getResources().getDisplayMetrics().heightPixels;
    int padding = (int) (width * PADDING_PERCENTAGE);

    CameraUpdate cu1 = CameraUpdateFactory.newLatLngBounds(bounds1, width, height, padding);
    mMap.animateCamera(cu1);
}