使用Open Graph API在Android中发布健身课程数据点位置

时间:2013-10-17 12:50:19

标签: android facebook facebook-graph-api geolocation

我正在开发Android健身应用程序,我正在尝试使用Open Graph API在Facebook上发布活动数据点位置。我使用了以下代码,它只发布了location1,但没有添加到列表中的其他位置。

public void shareWorkout (View v){
    if (FacebookDialog.canPresentShareDialog(getApplicationContext(), FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
        OpenGraphObject course = OpenGraphObject.Factory.createForPost(null);
        course.setType("fitness.course");
        course.setTitle("Walking Course");
        course.setUrl("http://samples.ogp.me/136756249803614");
        course.setDescription("walked a course");

        GraphObject location1 = GraphObject.Factory.create();
        location1.setProperty("latitude", 31.90592568833381f);
        location1.setProperty("longitude", 35.20052916370332f);
        location1.setProperty("altitude", 0.0f);

        GraphObject location2 = GraphObject.Factory.create();
        location2.setProperty("latitude", 31.912331976927817f);
        location2.setProperty("longitude", 35.205821581184864f);
        location2.setProperty("altitude", 0.0f);

        List<GraphObject> locations = new ArrayList<GraphObject>();
        locations.add(location1);
        locations.add(location2);

        GraphObject metrics = GraphObject.Factory.create();
        metrics.setProperty("location", locations);

        course.getData().setProperty("metrics", metrics);

        OpenGraphAction action = GraphObject.Factory.create(OpenGraphAction.class);
        action.setProperty("course", course);

        FacebookDialog shareDialog = new FacebookDialog.OpenGraphActionDialogBuilder(this,
        action, "fitness.walks", "course").build();            
        uiHelper.trackPendingDialogCall(shareDialog.present());
    }
}

2 个答案:

答案 0 :(得分:4)

我也尝试在facebook上分享课程。

与提到的facebook Submission Criteria一样,您应该包含尽可能多的适当指标(速度,地理位置,距离)。

因此,对于每个新位置,您必须创建一个新的度量标准对象,包括位置,时间戳,距离和速度,并将此metrics-Array添加到metric-Property。

以下是我的工作示例:

private void shareWorkout(Location[] locations) {
    NumberFormat doubleFormat = NumberFormat.getInstance(Locale.US);
    doubleFormat.setMaximumFractionDigits(6);
    doubleFormat.setMaximumIntegerDigits(3);
    doubleFormat.setGroupingUsed(false);

    OpenGraphObject course = OpenGraphObject.Factory
            .createForPost("fitness.course");
    course.setTitle("Sample Course");
    List<GraphObject> metrics = new ArrayList<GraphObject>();
    Location prevLocation = null;

    for (Location location : locations) {
        GraphObject metric = GraphObject.Factory.create();
        double distance = calcDistance(prevLocation, location);
        double speed = location.getSpeed() * LocationUtils.MS_TO_KMH;
        double pace = speed == 0 ? 0.0 : 60.0 / speed;
        metric.setProperty("location:longitude",
                doubleFormat.format(location.getLongitude()));
        metric.setProperty("location:latitude",
                doubleFormat.format(location.getLatitude()));
        metric.setProperty("location:altitude",
                doubleFormat.format(location.getAltitude()));
        metric.setProperty("timestamp",
                StringUtils.formatDateTimeIso8601(location.getTime()));
        metric.setProperty("distance:value", doubleFormat.format(distance));
        metric.setProperty("distance:units", "km");
        metric.setProperty("pace:value", doubleFormat.format(pace));
        metric.setProperty("pace:units", "s/m");
        metrics.add(metric);
        prevLocation = location;
    }
    GraphObject courseData = course.getData();
    courseData.setProperty("metrics", metrics);
    courseData.setProperty("distance:value", calcTime(locations));
    courseData.setProperty("distance:units", "min");
    courseData.setProperty("distance:value",
            doubleFormat.format(calcTotalDistance(locations)));
    courseData.setProperty("distance:units", "km");
    OpenGraphAction action = GraphObject.Factory
            .create(OpenGraphAction.class);
    action.setProperty("course", course);
    action.setType("fitness.walks");

    RequestBatch requestBatch = new RequestBatch();
    Request request = Request.newPostOpenGraphActionRequest(
            Session.getActiveSession(), action, shareCallback);
    requestBatch.add(request);
    requestBatch.executeAsync();
}

答案 1 :(得分:2)

从Facebook SDK 3.x更新到4.1时,我遇到了类似的问题。

在我的情况下,我已经拥有目标网址上的所有健身元数据(使用参考https://developers.facebook.com/docs/opengraph/guides/fitness创建),问题是关于共享(https://developers.facebook.com/docs/sharing/android#advanced)的文档有点误导。经过几次尝试后,我发现fitness:course也可以作为网址传递,而不仅仅是ShareGraphObject

ShareOpenGraphAction action = new ShareOpenGraphAction.Builder()
            .setActionType("fitness.runs")
            .putString("fitness:course", url)
            .build();

ShareOpenGraphContent content = new ShareOpenGraphContent.Builder()
            .setPreviewPropertyName("fitness:course")
            .setAction(action)
            .build();

打电话给

if (shareDialog.canShow(ShareOpenGraphContent.class)) {
    shareDialog.show(content);
}

希望这有帮助!