2016-01-06 117 views
2

我想以兩種方式在不同的時間做兩次地圖活動
01.在地圖上顯示兩個位置(假設當前用戶的位置和結束位置)。 02.在地圖上顯示幾個位置(用戶當前位置&幾個選定的位置)。在這兩種情況下,位置都是使用intent從另一個活動中提取的。當它以第二種方式使用時,它包含字符串數組。 如何過了時間,我得到一個錯誤說地圖活動未顯示地圖

「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) 
+0

我全局定義一個LatLngBounds.Builder如下LatLngBounds.Builder builder1 =新LatLngBounds.Builder(); –

回答

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); 
} 
在我的地圖活動