GoogleMap在Android中返回null和not null

时间:2016-11-25 09:38:07

标签: java android google-maps

我有以下名为 GoogleMapController 的类,它初始化GoogleMap并在已定义的位置绘制markers

GoogleMapController.java

class GoogleMapController {

    private static final String LOG_TAG = GoogleMapController.class.getSimpleName();

    private GoogleMap mGoogleMap;



    void initGoogleMap(final Activity activity, final int frameResID) {


                MapFragment mapFragment = MapFragment.newInstance();

                FragmentManager fragmentManager = activity.getFragmentManager();

                fragmentManager.beginTransaction().add(frameResID,mapFragment).commit();

                mapFragment.getMapAsync(new OnMapReadyCallback() {
                    @Override
                    public void onMapReady(GoogleMap googleMap) {

                        Log.d(LOG_TAG,"INIT==>"+String.valueOf(googleMap));
                        mGoogleMap = googleMap;
                    }
                });
    }

    void putLocationWithMarker(
            final Activity activity,
            final LatLng latLng,
            final String tag,
            final String title,
            final int markerResource) {



        Log.d(LOG_TAG,"LOC ==>"+String.valueOf(mGoogleMap));

                if (mGoogleMap != null) {
                    mGoogleMap.addMarker(
                            new MarkerOptions()
                                    .icon(BitmapDescriptorFactory.fromResource(markerResource))
                                    .title(title)
                                    .position(latLng))
                            .setTag(tag);

                    if (mUserLatLng != null) {
                        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mUserLatLng,15));
                    }
                }
                else
                    Log.e(LOG_TAG,"Google Map is NULL");
    }

}

我还有一个名为 GMapJSInterface 的类,它公开了在地图上对外部用户执行操作的功能

GMapJSInterface

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import com.google.android.gms.maps.model.LatLng;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class GMapJSInterface {

    private static final String LOG_TAG = GMapJSInterface.class.getSimpleName();

    private Activity mActivity;
    private GoogleMapController mGoogleMapController;

    public GMapJSInterface(final Activity activity) {
        mActivity = activity;
        mGoogleMapController = new GoogleMapController();

    }

   // @JavascriptInterface
    public void initGoogleMap(final String mapFrameIDStr) {

        if (mActivity != null) {
            if (!TextUtils.isEmpty(mapFrameIDStr)) {

                final int mapFrameIDint = Integer.parseInt(mapFrameIDStr);
                Log.i(LOG_TAG, "Frame ID: " + mapFrameIDint);


                        mGoogleMapController.initGoogleMap(mActivity, mapFrameIDint);


            } else {
                Log.e(LOG_TAG, "Error: EMPTY FRAME ID!!");
            }
        } else {
            Log.e(LOG_TAG,"Error: ACTIVITY NULL!");
        }
    }

    public void putLocations(final String locations) {

        Log.d(LOG_TAG,locations);

        try {
            final JSONArray locationArray = new JSONArray(locations);

            Log.d(LOG_TAG,String.valueOf(locationArray));
            for (int i = 0; i < locationArray.length(); i++) {
                final JSONObject location = locationArray.getJSONObject(i);

                final Double latitude = Double.parseDouble(location.getString("lat"));
                final Double longitude = Double.parseDouble(location.getString("lng"));
                final String tag = location.getString("tag");
                final String title = location.getString("title");

                final int resID = Integer.parseInt(location.getString("markerID"));

                mGoogleMapController.putLocationWithMarker(
                        mActivity,
                        new LatLng(latitude,longitude),
                        tag,title,resID);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


}

在我的 MainActivity.java 中,我只是执行以下操作: -

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GMapJSInterface gMapJSInterface = new GMapJSInterface(this);

        gMapJSInterface.initGoogleMap(String.valueOf(R.id.map_frame));

        final String locationArray =

        "[ \n " +
                "{ \"lat\" : \"12\", \n" +
                "  \"lng\" : \"12\", \n" +
                "  \"tag\" : \"0\", \n" +
                "  \"title\" : \"BLORE\", \n" +
                "  \"markerID\" : \""+String.valueOf(R.drawable.l_marker)+"\"\n" +
                "} \n" +
         "]";



        gMapJSInterface.putLocations(locationArray);

    }
}

这里的问题是:

  1. 当我调用GMapInterface类的initGoogleMap方法时,我确实在Android框架中打开了一张地图,我在logcat中得到以下内容
  2. 11-25 14:50:31.590 5831-5831/? D/GoogleMapController: INIT==>com.google.android.gms.maps.GoogleMap@1b9e89d4

    1. 当我在同一个类上调用putLocations时,我的logcat显示以下内容: -
    2. 11-25 14:50:30.850 5831-5831/? D/GoogleMapController: LOC ==>null 11-25 14:50:30.850 5831-5831/? E/GoogleMapController: Google Map is NULL

      我不明白我如何拥有与null相同的对象而不是null。

1 个答案:

答案 0 :(得分:1)

为什么不在onMapReady(GoogleMap map)回调中执行此操作?此时,地图不应为空。

您似乎正在尝试同步设置地图,然后将位置放入其中 - 有时它可以在您拨打putLocations时准备好,有时它不会成​​功。

据我所知,最好的办法是拨打getMapAsync并设置位置或抓取地图的引用,而在onMapReady中,这些地图会引人注目在最好的时候内存影响,所以使用回调!