2011-01-24 44 views
0

好吧,我正在編輯這個文件,以包括整個班級,並在過去的幾個小時內添加了一些新代碼。基本上,我希望用代表Facebook用戶簽入的標記填充Google Map。不幸的是,我的代碼並沒有合作 - 我試着審查Facebook提供的文檔,並在網上尋找答案,而沒有提供任何有用的東西。到目前爲止,我所能獲得的應用程序都是使用Facebook驗證應用程序的權限並顯示地圖,儘管我已經測試了在早期版本的應用程序中添加具有虛擬值的標記的能力,並且運行良好。爲什麼我對Facebook Graph API的調用不顯示任何內容?

我早先的問題涉及到我爲什麼對Graph API的調用沒有顯示任何內容 - 我做了與AuthorizeListener子類中列出的相同的調用,但僅嘗試在日誌中輸出原始JSON字符串而不是操縱它。我認爲不管造成這個問題的原因可能是我目前問題的原因。

無論如何,我如何讓我的應用程序顯示用戶簽入的位置標記?我認爲我的代碼讓我開始了一個很好的開始,但是在我的AuthorizeListener子類中顯然存在問題。你們有什麼感想?

public class FBCTActivity extends MapActivity { 
public static Context mContext; 
List<Overlay> mapOverlays; 
FBCTMarkerOverlay markerLayer; 
ArrayList<OverlayItem> overlays = new ArrayList<OverlayItem>(); 

// Facebook Application ID 
private static final String APP_ID = ""; 

Facebook mFacebook = new Facebook(APP_ID); 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mContext = this; 
    setContentView(R.layout.main); 

    // Set up Facebook stuff 
    mFacebook.authorize(this, new String[]{"user_checkins", "offline_access"}, new AuthorizeListener()); 

    // Set up map stuff 
    MapView mMapView = (MapView)findViewById(R.id.map); 
    mMapView.setSatellite(true); 
    MapController mMapController = mMapView.getController(); 
    mMapController.animateTo(getCurrentLocation()); 
    mMapController.setZoom(3); 

    // Set up overlay stuff 
    mapOverlays = mMapView.getOverlays(); 
    Drawable drawable = this.getResources().getDrawable(R.drawable.icon); 
    markerLayer = new FBCTMarkerOverlay(drawable); 

    // markerLayer is populated in the AuthorizeListener sub-class 
    mapOverlays.add(markerLayer); 

} 

/** 
* Determines the device's current location, but does not display it. 
* Used for centering the view on the device's location. 
* @return A GeoPoint object that contains the lat/long coordinates for the device's location. 
*/ 
private GeoPoint getCurrentLocation() { 
    LocationManager mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Criteria mCriteria = new Criteria(); 
    mCriteria.setAccuracy(Criteria.ACCURACY_COARSE); 
    mCriteria.setPowerRequirement(Criteria.POWER_LOW); 
    String mLocationProvider = mLocationManager.getBestProvider(mCriteria, true); 
    Location mLocation = mLocationManager.getLastKnownLocation(mLocationProvider); 

    int mLat = (int)(mLocation.getLatitude()*1E6); 
    int mLong = (int)(mLocation.getLongitude()*1E6); 
    return new GeoPoint(mLat, mLong); 
} 

@Override 
protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
} 

private class AuthorizeListener implements DialogListener { 
    public void onComplete(Bundle values) { 
     new Thread() { 
      @Override 
      public void run() { 
       try { 
        String response = mFacebook.request("me/checkins"); // The JSON to get 
              JSONObject jObject = Util.parseJson(response); 
        JSONArray jArray = jObject.getJSONArray("data"); // Read the JSON array returned by the request 
        for (int i = 0; i < jArray.length(); i++) { // Iterate through the array 
         JSONObject outerPlace = jArray.getJSONObject(i); // The outer JSON object 
         JSONObject place = outerPlace.getJSONObject("place"); // Second-tier JSON object that contains id, name, and location values for the "place" 
         String placeName = place.getString("name"); // The place's name 
         JSONObject placeLocation = place.getJSONObject("location"); // Third-tier JSON object that contains latitude and longitude coordinates for the place's "location" 
         int lat = (int) (placeLocation.getDouble("latitude")*1E6); // The place's latitude 
         int lon = (int) (placeLocation.getDouble("longitude")*1E6); // The place's longitude 
         String date = outerPlace.getString("created_time"); // Timestamp of the checkin 
         overlays.add(new OverlayItem(new GeoPoint(lat, lon), placeName, "Checked in on: " + date)); // Add the place's details to our ArrayList of OverlayItems 
        } 
        mFacebook.logout(mContext); // Logout of Facebook 
        for (int i = 0; i < overlays.size(); i++) { 
         markerLayer.addOverlayItem(overlays.get(i)); 
        } 
       } catch(IOException e) { 
        Log.v("FBCTActivity", e.getMessage()); 
       } catch(JSONException e) { 
        Log.v("FBCTActivity", e.getMessage()); 
       } 
      } 
     }.start(); 
    } 

    public void onFacebookError(FacebookError e) { 
     Log.w("FBCTActivity", e.getMessage()); 
     // TODO: Add more graceful error handling 
    } 

    public void onError(DialogError e) { 
     Log.w("FBCTActivity", e.getMessage()); 
    } 

    public void onCancel() { 
     // TODO Auto-generated method stub 

    } 
} 

}

+0

你認證了嗎? – Bozho 2011-01-24 22:40:44

回答

0

這可能不是原因,但你有沒有定義您的應用程序ID:

private static final String APP_ID = ""; 

此外,你必須重寫在調用該活動的onActivityResult mFacebook.authorize,因此將其添加到您的代碼中:

@Override 
protected void onActivityResult(int requestCode, int resultCode, 
           Intent data) { 
    mFacebook.authorizeCallback(requestCode, resultCode, data); 
} 

如果您不這樣做,您的應用程序將無法獲得Graph和您的連接的令牌將返回JSON錯誤消息。

相關問題