深入学习百度地图Android SDK v4.0.0【第十关】短链分享

时间:2022-11-13 01:38:18

深入学习百度地图Android SDK v4.0.0【第十关】短链分享

有的时候我们需要把自己所在的位置信息分享给朋友让其知道,方便对方来寻找自己!所以百度提供的短链分享还是非常有用的!

package cq.cake.chwl.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.share.OnGetShareUrlResultListener;
import com.baidu.mapapi.search.share.PoiDetailShareURLOption;
import com.baidu.mapapi.search.share.ShareUrlResult;
import com.baidu.mapapi.search.share.ShareUrlSearch;

import java.util.List;

import cq.cake.base.BaseToolbarMapActivity;
import cq.cake.chwl.R;
import cq.cake.util.L;

public class ShareActivity extends BaseToolbarMapActivity {
    ShareUrlSearch mShareUrlSearch;
    PoiSearch mPoiSearch;
    List<PoiInfo> allPoi;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        init();

    }

    private void init() {

         getUID();
        //1.创建分享检索实例
        mShareUrlSearch  = ShareUrlSearch.newInstance();
        //2.创建分享检索监听者
        OnGetShareUrlResultListener listener = new OnGetShareUrlResultListener() {
            public void onGetPoiDetailShareUrlResult(ShareUrlResult result) {
                //分享POI详情
                L.i("分享的短链=="+result.getUrl());
                sendUrl(ShareActivity.this,""+allPoi.get(result.describeContents()).name,result.getUrl());

            }
            public void onGetLocationShareUrlResult(ShareUrlResult result) {
                //分享位置信息

            }

            @Override
            public void onGetRouteShareUrlResult(ShareUrlResult shareUrlResult) {
            }
        };
        //3.设置分享检索监听者
        mShareUrlSearch.setOnGetShareUrlResultListener(listener);

    }

    public static void sendUrl(Context conxtext, String text, String url) {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, text + "\n" + url);
        sendIntent.setType("text/plain");
        conxtext.startActivity(sendIntent);
    }

    private void getUID() {
        mPoiSearch = PoiSearch.newInstance();
        mPoiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                 allPoi = poiResult.getAllPoi();
                String uid = allPoi.get(0).uid;
                mShareUrlSearch.requestPoiDetailShareUrl(new PoiDetailShareURLOption().poiUid(uid));
            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

            }

            @Override
            public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mShareUrlSearch.destroy();
        mPoiSearch.destroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_poi_share, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId) {
            case R.id.menu1:
                //先检索出来位置信息,然后分享位置
                mPoiSearch.searchInCity((new PoiCitySearchOption())
                        .city("重庆")
                        .keyword("渝中区大坪")
                        .pageNum(10));

                break;
        }
        return super.onOptionsItemSelected(item);
    }
}