2015-02-12 70 views
2

我想在for循環中聲明谷歌地圖標記。這裏是我的代碼:在for循環中創建谷歌地圖標記

public void latlong() throws JSONException { 

    JSONArray jsonArrayData = jsonData.getJSONArray("rows"); 

    for (int i = 0; i < jsonArrayData.length(); i++) { 

     JSONObject c = jsonArrayData.getJSONObject(i); 

     JSONObject tmpObject = c.getJSONObject("value"); 
     String latString = tmpObject.getString("latitude"); 
     String longString = tmpObject.getString("longitude"); 

     double lat = Double.parseDouble(latString); 
     double lng = Double.parseDouble(longString); 

     System.out.println("lat: " + lat + " // long: " + lng); 

     MarkerOptions place = new MarkerOptions().position(new LatLng(lat ,lng)).title("Hello Maps"); 
     googleMap.addMarker(place); 

    } 
} 

我不知道爲什麼我得到一個java.lang.NullPointerException。 感謝您的幫助!

的logcat:

02-12 01:19:02.363 9648-9648/emama.stadtrundgang E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: emama.stadtrundgang, PID: 9648 
    java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference 
      at emama.stadtrundgang.MapFragment.latlong(MapFragment.java:118) 
      at emama.stadtrundgang.MapFragment.onCreateView(MapFragment.java:40) 
      at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786) 
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947) 
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126) 
      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739) 
      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489) 
      at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486) 
      at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1073) 
      at android.support.v4.view.ViewPager.populate(ViewPager.java:919) 
      at android.support.v4.view.ViewPager$3.run(ViewPager.java:249) 
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) 
      at android.view.Choreographer.doCallbacks(Choreographer.java:580) 
      at android.view.Choreographer.doFrame(Choreographer.java:549) 
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 
      at android.os.Handler.handleCallback(Handler.java:739) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
+0

發佈您的logcat – Marcus 2015-02-12 00:25:48

回答

2

你的地圖似乎沒有被初始化。爲了擁有自己的地圖初始化,需要調用yourFragment.getMapAsync()在你的onCreate()方法,然後你會得到這樣的回調:

@Override 
public void onMapReady(GoogleMap map) { 
    map.addMarker(new MarkerOptions() 
     .position(new LatLng(10, 10)) 
     .title("Hello world")); 
} 

如果您的標記依賴於異步調用一個json資源,您可以在您的活動中設置一個標誌,如下所示:

boolean isMapReady = false; boolean isDataLoaded = false;

然後在onMapReady()和latlong()中,您可以檢查該標誌並調用例如addMarkers()方法。看下面的例子:

public void onMapReady(GoogleMap map) { 
    isMapReady = true; 
    if(isDataLoaded) 
     addMarkers(map, jsonData); 
} 

public void latLong() { 
    data = YOUR_DATA; 
    isDataLoaded = true; 

    if(isMapReady) 
     addMarkers(map, jsonData); 
} 
+0

我創建一個片段的谷歌地圖。所以我需要在我的onCreatView方法中調用yourFragment.getMapAsync()? – 2015-02-12 00:52:20

+0

您可以通過檢索片段並調用mapFragment.getMapAsync()或片段內部來調用它。這裏的示例https://developers.google.com/maps/documentation/android/ – Quanturium 2015-02-12 00:58:37

+0

對我的愚蠢感到抱歉,但是當我想在Fragments onCreateView方法中調用「getMapAsync(this)」時,它告訴我該方法無法解析。 – 2015-02-12 01:09:28

0

看來,當你嘗試將地圖上的標記,地圖處理不當被加載/初始化尚未

試圖調用虛擬方法com.google .android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker上一個空對象和借鑑(com.google.android.gms.maps.model.MarkerOptions)」