在 for 循环中创建 google 地图标记

creating google maps marker in a for loop

我想在 for 循环中声明 google 地图标记。这是我的代码:

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.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)

似乎当您尝试在地图上放置标记时,您的地图还没有loaded/initialized

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 referenc

您的地图似乎还没有初始化。为了初始化你的地图,你需要在你的 onCreate() 方法中调用 yourFragment.getMapAsync() 然后你会得到这样的回调:

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

如果您的标记依赖于对 json 资源的异步调用,您可以在 activity 中设置一个标志,如下所示:

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