Android百度地圖之短串分享

jopen 11年前發布 | 29K 次閱讀 Android Android開發 移動開發

在百度SDK中提供了一個短串分享功能,該功能核心意思就是,當你搜索到一個地址信息時,你可以通過短信、郵件、藍牙等發給你的好友或者分享到第三方平臺,包括空間、微信等等。而接收方可以直接通過收到的鏈接打開手機上的百度地圖客戶端或者手機瀏覽器進行查看,這樣就實現了地理位置信息的共享,百度Demo代碼如下:

注意:短串分享目前只開放“POI搜索結果分享”(本文默認搜索的是北京市的餐廳)和“反向地理編碼結果分享”(本文默認坐標是40.056878,116.308141),Demo只能按默認條件搜索是為方便演示,實際中應該從界面通過輸入獲取等才更具有靈活性。

介紹短串Activity(ShareDemo):

package com.home;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;

public class ShareDemo extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_demo);
    }

    public void startShareDemo(View view) {
        Intent intent = new Intent(this, ShareDemoActivity.class);
        startActivity(intent);
    }
}

ShareDemo布局文件(activity_share_demo):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="36dp"
        android:text="@string/share_tip"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:onClick="startShareDemo"
        android:text="開始體驗" />

</RelativeLayout>

其中字符串share_tip內容為:

<string name="share_tip">\t\t短串分享是指,用戶搜索查詢后得到的每一個地理位置結果將會對應一條短串(短鏈接),用戶可以通過短信、郵件或第三方分享組件(如微博、微信等)把短串分享給其他用戶從而實現地理位置信息的分享。當其他用戶收到分享的短串后,點擊短串即可打開手機上的百度地圖客戶端或者手機瀏覽器進行查看。\n\n
                \t\t例如,用戶搜索“百度大廈”后通過短信使用短串分享功能把該地點分享給好友,好友點擊短信中的短串“http://j.map.baidu.com/XLCrk”后可以調起百度地圖客戶端或者手機瀏覽器查看“百度大廈”的地理位置信息。\n\n
                \t\t目前短串分享功能暫時開放了“POI搜索結果分享”和“反向地理編碼結果分享”,日后會開放更多的功能,歡迎廣大開發者使用短串分享功能。</string>

主Activity(ShareDemoActivity):

package com.home;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.ItemizedOverlay;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.mapapi.map.PoiOverlay;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPoiInfo;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKShareUrlResult;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.baidu.platform.comapi.basestruct.GeoPoint;

/**
 * 演示poi搜索功能
 */
public class ShareDemoActivity extends Activity {

    private MapView mMapView = null;
    private MKSearch mSearch = null; // 搜索模塊,也可去掉地圖模塊獨立使用
    // 保存搜索結果地址
    private String currentAddr = null;
    // 搜索城市
    private String mCity = "北京";
    // 搜索關鍵字
    private String searchKey = "餐館";
    // 反地理編譯點坐標
    private GeoPoint mPoint = new GeoPoint((int) (40.056878 * 1E6),
            (int) (116.308141 * 1E6));

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DemoApplication app = (DemoApplication) this.getApplication();
        if (app.mBMapManager == null) {
            app.mBMapManager = new BMapManager(this);
            app.mBMapManager.init(DemoApplication.strKey,
                    new DemoApplication.MyGeneralListener());
        }
        setContentView(R.layout.activity_share_demo_activity);
        mMapView = (MapView) findViewById(R.id.bmapView);
        mMapView.getController().enableClick(true);
        mMapView.getController().setZoom(12);

        // 初始化搜索模塊,注冊搜索事件監聽
        mSearch = new MKSearch();
        mSearch.init(app.mBMapManager, new MKSearchListener() {

            @Override
            public void onGetPoiDetailSearchResult(int type, int error) {
            }

            /**
             * 在此處理poi搜索結果 , 用poioverlay 顯示
             */
            public void onGetPoiResult(MKPoiResult res, int type, int error) {
                // 錯誤號可參考MKEvent中的定義
                if (error != 0 || res == null) {
                    Toast.makeText(ShareDemoActivity.this, "抱歉,未找到結果",
                            Toast.LENGTH_LONG).show();
                    return;
                }
                // 將地圖移動到第一個POI中心點
                if (res.getCurrentNumPois() > 0) {
                    // 將poi結果顯示到地圖上
                    PoiShareOverlay poiOverlay = new PoiShareOverlay(
                            ShareDemoActivity.this, mMapView);
                    poiOverlay.setData(res.getAllPoi());
                    mMapView.getOverlays().clear();
                    mMapView.getOverlays().add(poiOverlay);
                    mMapView.refresh();
                    // 當ePoiType為2(公交線路)或4(地鐵線路)時, poi坐標為空
                    for (MKPoiInfo info : res.getAllPoi()) {
                        if (info.pt != null) {
                            mMapView.getController().animateTo(info.pt);
                            break;
                        }
                    }
                }
            }

            public void onGetDrivingRouteResult(MKDrivingRouteResult res,
                    int error) {
            }

            public void onGetTransitRouteResult(MKTransitRouteResult res,
                    int error) {
            }

            public void onGetWalkingRouteResult(MKWalkingRouteResult res,
                    int error) {
            }

            /**
             * 在此處理反地理編結果
             */
            public void onGetAddrResult(MKAddrInfo res, int error) {
                // 錯誤號可參考MKEvent中的定義
                if (error != 0 || res == null) {
                    Toast.makeText(ShareDemoActivity.this, "抱歉,未找到結果",
                            Toast.LENGTH_LONG).show();
                    return;
                }
                AddrShareOverlay addrOverlay = new AddrShareOverlay(
                        getResources().getDrawable(R.drawable.icon_marka),
                        mMapView, res);
                mMapView.getOverlays().clear();
                mMapView.getOverlays().add(addrOverlay);
                mMapView.refresh();

            }

            public void onGetBusDetailResult(MKBusLineResult result, int iError) {
            }

            @Override
            public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {
            }

            @Override
            public void onGetShareUrlResult(MKShareUrlResult result, int type,
                    int error) {
                // 分享短串結果
                Intent it = new Intent(Intent.ACTION_SEND);
                it.putExtra(Intent.EXTRA_TEXT, "您的朋友通過百度地圖SDK與您分享一個位置: "
                        + currentAddr + " -- " + result.url);
                it.setType("text/plain");
                startActivity(Intent.createChooser(it, "將短串分享到"));
            }
        });
    }

    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        mMapView.onResume();
        super.onResume();
    }

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

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mMapView.onRestoreInstanceState(savedInstanceState);
    }

    private void initMapView() {
        mMapView.setLongClickable(true);
        mMapView.getController().setZoom(14);
        mMapView.getController().enableClick(true);
        mMapView.setBuiltInZoomControls(true);
    }

    public void sharePoi(View view) {
        // 發起poi搜索
        mSearch.poiSearchInCity(mCity, searchKey);
        Toast.makeText(this, "在" + mCity + "搜索 " + searchKey,
                Toast.LENGTH_SHORT).show();
    }

    public void shareAddr(View view) {
        // 發起反地理編碼請求
        mSearch.reverseGeocode(mPoint);
        Toast.makeText(
                this,
                String.format("搜索位置: %f,%f", (mPoint.getLatitudeE6() * 1E-6),
                        (mPoint.getLongitudeE6() * 1E-6)), Toast.LENGTH_SHORT)
                .show();
    }

    /**
     * 使用PoiOverlay 展示poi點,在poi被點擊時發起短串請求.
     * 
     * @author kehongfeng
     * 
     */
    private class PoiShareOverlay extends PoiOverlay {

        public PoiShareOverlay(Activity activity, MapView mapView) {
            super(activity, mapView);
        }

        @Override
        protected boolean onTap(int i) {
            MKPoiInfo info = getPoi(i);
            currentAddr = info.address;
            mSearch.poiDetailShareURLSearch(info.uid);
            return true;
        }
    }

    /**
     * 使用ItemizevOvelray展示反地理編碼點位置,當該點被點擊時發起短串請求.
     * 
     */
    private class AddrShareOverlay extends ItemizedOverlay {

        private MKAddrInfo addrInfo;

        public AddrShareOverlay(Drawable defaultMarker, MapView mapView,
                MKAddrInfo addrInfo) {
            super(defaultMarker, mapView);
            this.addrInfo = addrInfo;
            addItem(new OverlayItem(addrInfo.geoPt, addrInfo.strAddr,
                    addrInfo.strAddr));
        }

        @Override
        public boolean onTap(int index) {
            currentAddr = addrInfo.strAddr;
            mSearch.poiRGCShareURLSearch(addrInfo.geoPt, "分享地址",
                    addrInfo.strAddr);
            return true;
        }

    }
}

其布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/poishore"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="2dip"
            android:layout_marginLeft="2dip"
            android:layout_marginRight="2dip"
            android:layout_marginTop="2dip"
            android:layout_weight="1"
            android:background="@drawable/button_style"
            android:onClick="sharePoi"
            android:padding="10dip"
            android:text="poi搜索結果分享" />

        <Button
            android:id="@+id/addrshare"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="2dip"
            android:layout_marginLeft="2dip"
            android:layout_marginRight="2dip"
            android:layout_marginTop="2dip"
            android:layout_weight="1"
            android:background="@drawable/button_style"
            android:onClick="shareAddr"
            android:text="反向地理編碼分享" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="點擊地圖上的搜索結果進行短串分享" />
    </LinearLayout>

    <com.baidu.mapapi.map.MapView
        android:id="@+id/bmapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true" />

</LinearLayout>

配置文件及Application類同前文。

附上圖片效果:

k1.png




k2.png

k3.png

 來自:http://blog.csdn.net/u010142437/article/details/11622099

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!