使用谷歌地图找到附近的地方并放置Api

时间:2013-06-30 05:36:05

标签: android maps google-places-api

我是android的新手。我跟着一个关于使用地方api寻找附近地方的尝试,并尝试将其集成到我的应用程序中。我在我的应用程序的主页活动中传递了一个意图,例如,“Intent i1 = new Intent(this,NearbyPlaces.class); startactivity(i1);”但它不起作用,当我点击调用附近活动意图的按钮时,应用程序意外关闭。不知道wat是错的,因为代码与教程中提到的几乎相同。以下是我的代码。如果有人可以提供帮助,我将非常感激......

//code for NearbyPlaces.java

package com.example.travelplanner;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;


public class NearbyPlacesActivity extends Activity {

    // flag for Internet connection status
    Boolean isInternetPresent = false;

    // Connection detector class
    ConnectionDetector cd;

    // Alert Dialog Manager
    AlertDialogManager alert = new AlertDialogManager();

    // Google Places
    GooglePlaces googlePlaces;

    // Places List
    PlacesList nearPlaces;

    // GPS Location
    GPSTracker gps;

    // Button
    Button btnShowOnMap;

    // Progress dialog
    ProgressDialog pDialog;

    // Places Listview
    ListView lv;

    // ListItems data
    ArrayList<HashMap<String, String>> placesListItems = new ArrayList<HashMap<String,String>>();


    // KEY Strings
    public static String KEY_REFERENCE = "reference"; // id of the place
    public static String KEY_NAME = "name"; // name of the place
    public static String KEY_VICINITY = "vicinity"; // Place area name

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

        cd = new ConnectionDetector(getApplicationContext());

        // Check if Internet present
        isInternetPresent = cd.isConnectingToInternet();
        if (!isInternetPresent) {
            // Internet Connection is not present
            alert.showAlertDialog(NearbyPlacesActivity.this, "Internet Connection Error",
                    "Please connect to working Internet connection", false);
            // stop executing code by return
            return;
        }

        // creating GPS Class object
        gps = new GPSTracker(this);

        // check if GPS location can get
        if (gps.canGetLocation()) {
            Log.d("Your Location", "latitude:" + gps.getLatitude() + ", longitude: " + gps.getLongitude());
        } else {
            // Can't get user's current location
            alert.showAlertDialog(NearbyPlacesActivity.this, "GPS Status",
                    "Couldn't get location information. Please enable GPS",
                    false);
            // stop executing code by return
            return;
        }

        // Getting listview
        lv = (ListView) findViewById(R.id.list);

        // button show on map
        btnShowOnMap = (Button) findViewById(R.id.btn_show_map);

        // calling background Async task to load Google Places
        // After getting places from Google all the data is shown in listview
        new LoadPlaces().execute();

        /** Button click event for shown on map */
        btnShowOnMap.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(getApplicationContext(),
                        PlacesMapActivity.class);
                // Sending user current geo location
                i.putExtra("user_latitude", Double.toString(gps.getLatitude()));
                i.putExtra("user_longitude", Double.toString(gps.getLongitude()));

                // passing near places to map activity
                i.putExtra("near_places", nearPlaces);
                // staring activity
                startActivity(i);
            }
        });


        /**
         * ListItem click event
         * On selecting a listitem SinglePlaceActivity is launched
         * */
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
                String reference = ((TextView) view.findViewById(R.id.reference)).getText().toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        SinglePlaceActivity.class);

                // Sending place refrence id to single place activity
                // place refrence id used to get "Place full details"
                in.putExtra(KEY_REFERENCE, reference);
                startActivity(in);
            }
        });
    }

    /**
     * Background Async Task to Load Google places
     * */
    class LoadPlaces extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(NearbyPlacesActivity.this);
            pDialog.setMessage(Html.fromHtml("<b>Search</b><br/>Loading Places..."));
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting Places JSON
         * */
        protected String doInBackground(String... args) {
            // creating Places class object
            googlePlaces = new GooglePlaces();

            try {
                // Separeate your place types by PIPE symbol "|"
                // If you want all types places make it as null
                // Check list of types supported by google
                // 
                String types = "cafe|restaurant"; // Listing places only cafes, restaurants

                // Radius in meters - increase this value if you don't find any places
                double radius = 1000; // 1000 meters 

                // get nearest places
                nearPlaces = googlePlaces.search(gps.getLatitude(),
                        gps.getLongitude(), radius, types);


            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * and show the data in UI
         * Always use runOnUiThread(new Runnable()) to update UI from background
         * thread, otherwise you will get error
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed Places into LISTVIEW
                     * */
                    // Get json response status
                    String status = nearPlaces.status;

                    // Check for all possible status
                    if(status.equals("OK")){
                        // Successfully got places details
                        if (nearPlaces.results != null) {
                            // loop through each place
                            for (Place p : nearPlaces.results) {
                                HashMap<String, String> map = new HashMap<String, String>();

                                // Place reference won't display in listview - it will be hidden
                                // Place reference is used to get "place full details"
                                map.put(KEY_REFERENCE, p.reference);

                                // Place name
                                map.put(KEY_NAME, p.name);


                                // adding HashMap to ArrayList
                                placesListItems.add(map);
                            }
                            // list adapter
                            ListAdapter adapter = new SimpleAdapter(NearbyPlacesActivity.this, placesListItems,
                                    R.layout.list_item,
                                    new String[] { KEY_REFERENCE, KEY_NAME}, new int[] {
                                            R.id.reference, R.id.name });

                            // Adding data into listview
                            lv.setAdapter(adapter);
                        }
                    }
                    else if(status.equals("ZERO_RESULTS")){
                        // Zero results found
                        alert.showAlertDialog(NearbyPlacesActivity.this, "Near Places",
                                "Sorry no places found. Try to change the types of places",
                                false);
                    }
                    else if(status.equals("UNKNOWN_ERROR"))
                    {
                        alert.showAlertDialog(NearbyPlacesActivity.this, "Places Error",
                                "Sorry unknown error occured.",
                                false);
                    }
                    else if(status.equals("OVER_QUERY_LIMIT"))
                    {
                        alert.showAlertDialog(NearbyPlacesActivity.this, "Places Error",
                                "Sorry query limit to google places is reached",
                                false);
                    }
                    else if(status.equals("REQUEST_DENIED"))
                    {
                        alert.showAlertDialog(NearbyPlacesActivity.this, "Places Error",
                                "Sorry error occured. Request is denied",
                                false);
                    }
                    else if(status.equals("INVALID_REQUEST"))
                    {
                        alert.showAlertDialog(NearbyPlacesActivity.this, "Places Error",
                                "Sorry error occured. Invalid Request",
                                false);
                    }
                    else
                    {
                        alert.showAlertDialog(NearbyPlacesActivity.this, "Places Error",
                                "Sorry error occured.",
                                false);
                    }
                }
            });

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.nearby_places, menu);
        return true;
    }



}

下面是我的logcat:

06-30 05:25:55.315: D/dalvikvm(1045): GC_FOR_ALLOC freed 34K, 7% free 2424K/2580K, paused 87ms, total 91ms
06-30 05:25:55.415: I/dalvikvm-heap(1045): Grow heap (frag case) to 7.125MB for 4869136-byte allocation
06-30 05:25:55.507: D/dalvikvm(1045): GC_FOR_ALLOC freed 1K, 3% free 7178K/7336K, paused 84ms, total 84ms
06-30 05:25:55.635: D/dalvikvm(1045): GC_CONCURRENT freed <1K, 3% free 7178K/7336K, paused 12ms+31ms, total 133ms
06-30 05:25:56.575: D/gralloc_goldfish(1045): Emulator without GPU emulation detected.
06-30 05:25:57.526: I/Choreographer(1045): Skipped 184 frames!  The application may be doing too much work on its main thread.
06-30 05:25:58.025: I/Choreographer(1045): Skipped 130 frames!  The application may be doing too much work on its main thread.
06-30 05:25:58.825: I/Choreographer(1045): Skipped 31 frames!  The application may be doing too much work on its main thread.
06-30 05:26:00.065: I/Choreographer(1045): Skipped 31 frames!  The application may be doing too much work on its main thread.
06-30 05:26:00.735: I/Choreographer(1045): Skipped 30 frames!  The application may be doing too much work on its main thread.
06-30 05:26:00.965: I/Choreographer(1045): Skipped 44 frames!  The application may be doing too much work on its main thread.
06-30 05:26:01.105: I/Choreographer(1045): Skipped 36 frames!  The application may be doing too much work on its main thread.
06-30 05:26:01.426: I/Choreographer(1045): Skipped 34 frames!  The application may be doing too much work on its main thread.
06-30 05:26:01.605: I/Choreographer(1045): Skipped 46 frames!  The application may be doing too much work on its main thread.
06-30 05:26:02.434: D/dalvikvm(1045): GC_FOR_ALLOC freed 15K, 2% free 7591K/7728K, paused 325ms, total 343ms
06-30 05:26:03.665: D/dalvikvm(1045): GC_CONCURRENT freed 2K, 2% free 9210K/9348K, paused 75ms+61ms, total 402ms
06-30 05:26:03.675: D/dalvikvm(1045): WAIT_FOR_CONCURRENT_GC blocked 330ms
06-30 05:26:04.235: I/Choreographer(1045): Skipped 685 frames!  The application may be doing too much work on its main thread.
06-30 05:26:06.815: I/Choreographer(1045): Skipped 669 frames!  The application may be doing too much work on its main thread.
06-30 05:26:08.095: I/Choreographer(1045): Skipped 333 frames!  The application may be doing too much work on its main thread.
06-30 05:26:08.835: I/Choreographer(1045): Skipped 103 frames!  The application may be doing too much work on its main thread.
06-30 05:26:09.045: I/Choreographer(1045): Skipped 37 frames!  The application may be doing too much work on its main thread.
06-30 05:26:09.494: D/dalvikvm(1045): GC_FOR_ALLOC freed 1605K, 18% free 8393K/10120K, paused 89ms, total 105ms
06-30 05:26:09.694: I/Choreographer(1045): Skipped 86 frames!  The application may be doing too much work on its main thread.
06-30 05:26:13.365: D/dalvikvm(1045): GC_FOR_ALLOC freed 1569K, 18% free 8389K/10120K, paused 147ms, total 191ms
06-30 05:26:13.485: I/dalvikvm-heap(1045): Grow heap (frag case) to 10.092MB for 1872016-byte allocation
06-30 05:26:13.666: D/dalvikvm(1045): GC_FOR_ALLOC freed 5137K, 58% free 5079K/11952K, paused 177ms, total 177ms
06-30 05:26:17.095: I/Choreographer(1045): Skipped 56 frames!  The application may be doing too much work on its main thread.
06-30 05:26:17.695: D/dalvikvm(1045): GC_FOR_ALLOC freed 1831K, 73% free 3261K/11952K, paused 356ms, total 389ms
06-30 05:26:17.704: I/dalvikvm-heap(1045): Grow heap (frag case) to 4.794MB for 1568016-byte allocation
06-30 05:26:18.304: D/dalvikvm(1045): GC_CONCURRENT freed <1K, 60% free 4791K/11952K, paused 78ms+73ms, total 601ms
06-30 05:26:20.645: I/Choreographer(1045): Skipped 544 frames!  The application may be doing too much work on its main thread.
06-30 05:26:22.796: I/Choreographer(1045): Skipped 444 frames!  The application may be doing too much work on its main thread.
06-30 05:26:23.715: D/dalvikvm(1045): GC_FOR_ALLOC freed 1556K, 66% free 3456K/10120K, paused 196ms, total 224ms
06-30 05:26:23.755: I/dalvikvm-heap(1045): Grow heap (frag case) to 5.015MB for 1600016-byte allocation
06-30 05:26:24.015: D/dalvikvm(1045): GC_CONCURRENT freed 10K, 51% free 5008K/10120K, paused 78ms+25ms, total 254ms
06-30 05:26:24.125: I/Choreographer(1045): Skipped 247 frames!  The application may be doing too much work on its main thread.
06-30 05:26:25.358: D/dalvikvm(1045): GC_FOR_ALLOC freed 1567K, 66% free 3447K/10120K, paused 154ms, total 173ms
06-30 05:26:25.386: I/dalvikvm-heap(1045): Grow heap (frag case) to 5.007MB for 1600016-byte allocation
06-30 05:26:25.804: D/dalvikvm(1045): GC_CONCURRENT freed 1K, 51% free 5008K/10120K, paused 97ms+42ms, total 421ms
06-30 05:26:29.347: D/dalvikvm(1045): GC_FOR_ALLOC freed 1563K, 66% free 3447K/10120K, paused 164ms, total 174ms
06-30 05:26:29.435: I/dalvikvm-heap(1045): Grow heap (frag case) to 5.266MB for 1872016-byte allocation
06-30 05:26:29.767: D/dalvikvm(1045): GC_CONCURRENT freed <1K, 48% free 5274K/10120K, paused 84ms+35ms, total 330ms
06-30 05:26:33.317: D/dalvikvm(1045): GC_FOR_ALLOC freed 1830K, 66% free 3452K/10120K, paused 153ms, total 158ms
06-30 05:26:33.344: I/dalvikvm-heap(1045): Grow heap (frag case) to 4.981MB for 1568016-byte allocation
06-30 05:26:33.784: D/dalvikvm(1045): GC_CONCURRENT freed <1K, 51% free 4982K/10120K, paused 117ms+26ms, total 437ms
06-30 05:26:37.675: D/dalvikvm(1045): GC_FOR_ALLOC freed 1534K, 66% free 3473K/10120K, paused 171ms, total 178ms
06-30 05:26:37.725: I/dalvikvm-heap(1045): Grow heap (frag case) to 5.033MB for 1600016-byte allocation
06-30 05:26:38.015: D/dalvikvm(1045): GC_CONCURRENT freed 1K, 51% free 5034K/10120K, paused 80ms+24ms, total 293ms
06-30 05:26:41.355: D/dalvikvm(1045): GC_FOR_ALLOC freed 1564K, 66% free 3473K/10120K, paused 156ms, total 160ms
06-30 05:26:41.385: I/dalvikvm-heap(1045): Grow heap (frag case) to 5.033MB for 1600016-byte allocation
06-30 05:26:41.774: D/dalvikvm(1045): GC_CONCURRENT freed <1K, 51% free 5035K/10120K, paused 104ms+23ms, total 388ms
06-30 05:26:44.245: I/Choreographer(1045): Skipped 49 frames!  The application may be doing too much work on its main thread.
06-30 05:26:45.255: I/Choreographer(1045): Skipped 164 frames!  The application may be doing too much work on its main thread.
06-30 05:26:46.315: D/dalvikvm(1045): GC_FOR_ALLOC freed 1565K, 66% free 3475K/10120K, paused 176ms, total 181ms
06-30 05:26:46.335: I/dalvikvm-heap(1045): Grow heap (frag case) to 5.293MB for 1872016-byte allocation
06-30 05:26:46.595: D/dalvikvm(1045): GC_CONCURRENT freed 1K, 48% free 5302K/10120K, paused 78ms+25ms, total 258ms
06-30 05:26:46.697: I/Choreographer(1045): Skipped 281 frames!  The application may be doing too much work on its main thread.
06-30 05:26:47.116: I/Choreographer(1045): Skipped 62 frames!  The application may be doing too much work on its main thread.
06-30 05:26:47.490: I/Choreographer(1045): Skipped 38 frames!  The application may be doing too much work on its main thread.
06-30 05:26:48.515: I/Choreographer(1045): Skipped 39 frames!  The application may be doing too much work on its main thread.
06-30 05:26:49.365: D/dalvikvm(1045): GC_FOR_ALLOC freed 1844K, 60% free 4132K/10120K, paused 109ms, total 129ms
06-30 05:26:49.385: I/dalvikvm-heap(1045): Grow heap (frag case) to 5.645MB for 1568016-byte allocation
06-30 05:26:49.717: D/dalvikvm(1045): GC_CONCURRENT freed 188K, 46% free 5474K/10120K, paused 82ms+15ms, total 326ms
06-30 05:26:49.755: I/Choreographer(1045): Skipped 145 frames!  The application may be doing too much work on its main thread.
06-30 05:26:53.785: D/dalvikvm(1045): GC_FOR_ALLOC freed 14K, 44% free 5698K/10120K, paused 370ms, total 378ms
06-30 05:26:54.406: I/dalvikvm-heap(1045): Grow heap (frag case) to 10.323MB for 4869136-byte allocation
06-30 05:26:55.017: D/dalvikvm(1045): GC_CONCURRENT freed 4K, 30% free 10448K/14876K, paused 16ms+113ms, total 606ms
06-30 05:26:57.294: D/GPS(1045): GPS Enabled
06-30 05:26:57.336: D/Your Location(1045): latitude:0.0, longitude: 0.0
06-30 05:26:58.475: D/AndroidRuntime(1045): Shutting down VM
06-30 05:26:58.475: W/dalvikvm(1045): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-30 05:26:58.555: E/AndroidRuntime(1045): FATAL EXCEPTION: main
06-30 05:26:58.555: E/AndroidRuntime(1045): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.travelplanner/com.example.travelplanner.NearbyPlacesActivity}: java.lang.NullPointerException
06-30 05:26:58.555: E/AndroidRuntime(1045):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-30 05:26:58.555: E/AndroidRuntime(1045):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-30 05:26:58.555: E/AndroidRuntime(1045):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-30 05:26:58.555: E/AndroidRuntime(1045):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-30 05:26:58.555: E/AndroidRuntime(1045):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 05:26:58.555: E/AndroidRuntime(1045):     at android.os.Looper.loop(Looper.java:137)
06-30 05:26:58.555: E/AndroidRuntime(1045):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-30 05:26:58.555: E/AndroidRuntime(1045):     at java.lang.reflect.Method.invokeNative(Native Method)
06-30 05:26:58.555: E/AndroidRuntime(1045):     at java.lang.reflect.Method.invoke(Method.java:511)
06-30 05:26:58.555: E/AndroidRuntime(1045):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-30 05:26:58.555: E/AndroidRuntime(1045):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-30 05:26:58.555: E/AndroidRuntime(1045):     at dalvik.system.NativeStart.main(Native Method)
06-30 05:26:58.555: E/AndroidRuntime(1045): Caused by: java.lang.NullPointerException
06-30 05:26:58.555: E/AndroidRuntime(1045):     at com.example.travelplanner.NearbyPlacesActivity.onCreate(NearbyPlacesActivity.java:109)
06-30 05:26:58.555: E/AndroidRuntime(1045):     at android.app.Activity.performCreate(Activity.java:5104)
06-30 05:26:58.555: E/AndroidRuntime(1045):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-30 05:26:58.555: E/AndroidRuntime(1045):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-30 05:26:58.555: E/AndroidRuntime(1045):     ... 11 more
06-30 05:26:59.336: I/dalvikvm(1045): Could not find method com.google.common.base.Preconditions.checkNotNull, referenced from method com.google.api.client.http.GenericUrl.setHost
06-30 05:26:59.385: W/dalvikvm(1045): VFY: unable to resolve static method 8238: Lcom/google/common/base/Preconditions;.checkNotNull (Ljava/lang/Object;)Ljava/lang/Object;
06-30 05:26:59.415: D/dalvikvm(1045): VFY: replacing opcode 0x71 at 0x0000
06-30 05:26:59.455: I/dalvikvm(1045): Could not find method com.google.common.base.Preconditions.checkArgument, referenced from method com.google.api.client.http.GenericUrl.setPort
06-30 05:26:59.475: W/dalvikvm(1045): VFY: unable to resolve static method 8236: Lcom/google/common/base/Preconditions;.checkArgument (ZLjava/lang/Object;)V
06-30 05:26:59.475: D/dalvikvm(1045): VFY: replacing opcode 0x71 at 0x0006
06-30 05:26:59.546: I/dalvikvm(1045): Could not find method com.google.common.base.Preconditions.checkNotNull, referenced from method com.google.api.client.http.GenericUrl.setScheme
06-30 05:26:59.632: W/dalvikvm(1045): VFY: unable to resolve static method 8238: Lcom/google/common/base/Preconditions;.checkNotNull (Ljava/lang/Object;)Ljava/lang/Object;
06-30 05:26:59.632: D/dalvikvm(1045): VFY: replacing opcode 0x71 at 0x0000
06-30 05:27:02.425: I/Process(1045): Sending signal. PID: 1045 SIG: 9

我是否应该指定用于查找附近地点的其他类代码? 并且请作为一个新手,我期待一些更简单的解释......我不太了解Android世界..

0 个答案:

没有答案