使用Google Places API在Google Maps中查找附近的地点 [英] Find Places Nearby in Google Maps using Google Places API

查看:60
本文介绍了使用Google Places API在Google Maps中查找附近的地点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

本Android教程旨在学习如何使用Google Places API在Google地图中查找附近的地点.该应用程序将运行,然后单击按钮,它将不传递googlePlacesJson值,并在那里返回null.

This Android tutorial is to learn about using Google Places API to find places nearby in Google maps. Ones the app runs then and click the button it wont pass the googlePlacesJson values and there by it returns 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

推荐答案

请检查下面的代码

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

这篇关于使用Google Places API在Google Maps中查找附近的地点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆