使用Google Places API在Google地图中查找附近的地点

时间:2015-12-14 11:26:52

标签: android google-maps

http://javapapers.com/android/find-places-nearby-in-google-maps-using-google-places-apiandroid-app/

此Android教程旨在了解如何使用Google Places API在Google地图中查找附近的地点。应用程序运行然后单击按钮它不会传递googlePlacesJson值,然后返回null。

12-14 15:06:16.266 6095-6095/com.example.tony_.test_no E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tony_.test_no, PID: 6095
java.lang.NullPointerException
    at com.example.tony_.test_no.PlacesDisplayTask.onPostExecute(PlacesDisplayTask.java:42)
    at com.example.tony_.test_no.PlacesDisplayTask.onPostExecute(PlacesDisplayTask.java:18)
    at android.os.AsyncTask.finish(AsyncTask.java:632)
    at android.os.AsyncTask.access$600(AsyncTask.java:177)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5867)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
    at dalvik.system.NativeStart.main(Native Method)
12-14 15:06:17.938 6095-6095/com.example.tony_.test_no D/Process: killProcess, pid=6095

1 个答案:

答案 0 :(得分:2)

请检查以下代码

public class PlacesDisplayTask extends AsyncTask<Object, Integer, List<HashMap<String, String>>> {

        JSONObject googlePlacesJson;
        GoogleMap googleMap;

        @Override
        protected List<HashMap<String, String>> doInBackground(Object... inputObj) {

            List<HashMap<String, String>> googlePlacesList = null;
            Places placeJsonParser = new Places();

            try {
                googleMap = (GoogleMap) inputObj[0];
                googlePlacesJson = new JSONObject((String) inputObj[1]);
                googlePlacesList = placeJsonParser.parse(googlePlacesJson);
            } catch (Exception e) {
                Log.d("Exception", e.toString());
            }
            return googlePlacesList;
        }

        @Override
        protected void onPostExecute(List<HashMap<String, String>> list) {
            googleMap.clear();
            for (int i = 0; i < list.size(); i++) {
                MarkerOptions markerOptions = new MarkerOptions();
                HashMap<String, String> googlePlace = list.get(i);
                double lat = Double.parseDouble(googlePlace.get("lat"));
                double lng = Double.parseDouble(googlePlace.get("lng"));
                String placeName = googlePlace.get("place_name");
                String vicinity = googlePlace.get("vicinity");
                LatLng latLng = new LatLng(lat, lng);
                markerOptions.position(latLng);
                markerOptions.title(placeName + " : " + vicinity);
                googleMap.addMarker(markerOptions);
            }
        }
    }