1

我正在构建一个应用程序,我们可以在其中找到最近的商店,这些商店的电话号码已在 google 地方注册。我正在使用此地址中可用的一组相当知名的代码 - http://www.androidhive.info/2012/08/android-working-with-google-places-and-maps-tutorial/

现在,当我按原样运行下载的代码时(仅包括“rankby”而不是“radius”),代码运行良好。但是为了用电话号码过滤结果,我在其中添加了一个“if”。然后它根本不返回任何结果。我发现当我从 MainActivity 调用 GooglePlaces.getPlaceDetails(reference) 时,它会引发异常。但从 SinglePlaceActivity 它工作正常。我们不能从一个 Activity 调用 Google Place API 两次吗?

我还面临的第二个问题是,如果我将 MainActivity(没有实现 if 子句)更改为其他一些活动,并从新的 MainActivity 调用它而不传递任何内容,它会在应用程序关闭后给出错误。我认为这两个问题是相互关联的,所以我一起问。

提前致谢...

更改后 My MainActivity.java 的必需部分

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

try {
    String types = "store"; // Listing places only ambulances
    // get nearest places
    nearPlaces = googlePlaces.search(gps.getLatitude(), gps.getLongitude(), types);

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


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
                googlePlaceDetails = new GooglePlaces();
                for (Place p : nearPlaces.results) {
                    try {
                        PlaceID = p.reference;
                        placeDetails =  googlePlaceDetails.getPlaceDetails(PlaceID);
                        txtPhone = placeDetails.result.formatted_phone_number;
                    } catch (Exception e)
                    {

                    }
                    if (txtPhone!=null){
                        HashMap<String, String> map = new HashMap<String, String>();

                        map.put(KEY_REFERENCE, p.reference);

                            map.put(KEY_NAME, p.name);


                        placesListItems.add(map);
                    }
                }
                ListAdapter adapter = new SimpleAdapter(MainActivity.this, placesListItems,
                R.layout.list_item,
                new String[] { KEY_REFERENCE, KEY_NAME}, new int[] {
                         R.id.reference, R.id.name });

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

    }

}

我没有更改我的 GooglePlaces.java 文件,因为这是一个众所周知的代码,所以我没有上传它。如果有人可以帮助我,那将对我有很大的帮助...

再次,提前感谢。

4

1 回答 1

0

这里有一个完整的谷歌地方 API 代码,阅读代码末尾的注释。

public class PlacesAPIActivity extends AppCompatActivity {

private int PLACE_PICKER_REQUEST = 1;

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

            PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
            Intent intent;
            try {
                intent = builder.build(getApplicationContext());
                startActivityForResult(intent, PLACE_PICKER_REQUEST);
            } catch (GooglePlayServicesRepairableException e) {
                e.printStackTrace();
            } catch (GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
            }

        }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_place_api, menu);

    return true;
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PLACE_PICKER_REQUEST) {
        if (resultCode == RESULT_OK) {

            Place place = PlacePicker.getPlace(data , this) ;

            String toastMsg = String.format("Place: %s %s", place.getName(), place.getPhoneNumber()); // HERE YOU GET THE NUMBER PHONE.
            Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
        }
    }
}

}

于 2015-10-10T23:01:37.820 回答