2013-08-17 66 views
1

我正在構建一個應用程序,在該應用程序中我們可以找到最近的商店,該地址的電話號碼已在谷歌地點註冊。我在這個地址中使用了一組相當已知的代碼 - http://www.androidhive.info/2012/08/android-working-with-google-places-and-maps-tutorial/獲取帶有電話號碼的Google地方信息結果

現在,當我按照原樣運行下載的代碼時(只包括「rankby」而不是「radius」),代碼運行良好。但爲了用電話號碼過濾結果,我在其中添加了一個'if'。然後它根本沒有返回任何結果。我發現當我從MainActivity調用GooglePlaces.getPlaceDetails(引用)時,它拋出一個異常。但從SinglePlaceActivity它工作正常。我們不能從單個Activity調用兩次Google place API嗎?

我還面臨的第二個問題是,如果我將MainActivity(不執行if子句)更改爲其他某個活動,並在不傳遞任何內容的情況下從新的MainActivity調用它,隨後應用程序關閉時會發出錯誤。我認爲這兩個問題是相互關聯的,所以我一起問。

在此先感謝...

變更後我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

Schakraborty我也遵循同樣的教程,但我被困在某個地方,所以我跟着這個鏈接,但只生成附近的地方,你可以實現其他詳細資料,如電話號碼.... HTTP ://www.androidbegin.com/tutorial/implementing-actionbarsherlock-search-collapsible-view-in-android/ – Aravin

+0

參考這個獲取電話號碼http://stackoverflow.com/questions/14028128/displaying-formatted-phone- google-places-a-alert-dialog-o?rq = 1 – Aravin

+0

感謝Aravinth,但我已經通過了你提供的第二個鏈接,但找不到任何有用的東西我。關於第一個鏈接,我認爲你錯誤地發佈了其他鏈接。 – sChakraborty

回答

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

}