我正在建立一个应用程序,我们可以在那里找到最近的商店,这些商店的电话号码已在谷歌注册。我在这个地址中使用了一组相当已知的代码 - http://www.androidhive.info/2012/08/android-working-with-google-places-and-maps-tutorial/。
现在,当我按原样运行下载的代码时(仅包括“rankby”而不是“radius”),代码运行正常。但是为了用电话号码过滤结果,我在其中添加了“if”。然后它根本没有返回任何结果。我发现当我从MainActivity调用GooglePlaces.getPlaceDetails(引用)时,它会抛出异常。但是从SinglePlaceActivity它工作正常。我们不能从单个活动中拨打两次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文件,因为这是一个众所周知的代码,我没有上传它。如果有人能帮助我,那将对我有很大的帮助......
再次,先谢谢。
答案 0 :(得分:0)
这里有一个完整的google places 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();
}
}
}
}