BaiduMap---百度地图官方Demo之短串分享功能(介绍关键词查询,suggestion查询和查看餐饮类Place详情页功能)

<?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
        xmlns:android="http://schemas.android.com/apk/res/android"
        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
        xmlns:android="http://schemas.android.com/apk/res/android"
        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>




package baidumapsdk.demo;

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

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.overlayutil.PoiOverlay;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
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.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.share.LocationShareURLOption;
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;

/**
 * 演示poi搜索功能
 */
public class ShareDemoActivity extends Activity implements
		OnGetPoiSearchResultListener, OnGetShareUrlResultListener,
		OnGetGeoCoderResultListener, BaiduMap.OnMarkerClickListener {

	private MapView mMapView = null;
	
	/**
	 * Point of Interest 检索接口
	 * */
	private PoiSearch mPoiSearch = null; // 搜索模块,也可去掉地图模块独立使用
	
	/**
	 * 共享URL查询接口 
	 * 
	 * 共享URL是指代表特定信息(poi详情/位置)的一条经过压缩的URL,
	 * 该URL可通过任何途径传播。 当终端用户点击该URL时,
	 * 该URL所代表的特定信息会通过百度地图(客户端/web)重新呈现 
	 * */
	private ShareUrlSearch mShareUrlSearch = null;
	
	/**
	 * 地理编码查询接口
	 * */
	private GeoCoder mGeoCoder = null;
	
	// 保存搜索结果地址
	private String currentAddr = null;
	// 搜索城市
	private String mCity = "北京";
	// 搜索关键字
	private String searchKey = "餐馆";
	// 反地理编译点坐标
	private LatLng mPoint = new LatLng(40.056878, 116.308141);
	private BaiduMap mBaiduMap = null;
	/**
	 * 定义地图 Marker 覆盖物
	 * */
	private Marker mAddrMarker = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_share_demo_activity);
		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();
		
		/**
		 * 创建PoiSearch实例
		 * */
		mPoiSearch = PoiSearch.newInstance();
		
		/**
		 * public void setOnGetPoiSearchResultListener(OnGetPoiSearchResultListener listener)
		 * 设置poi检索监听者
		 * @param listener - 监听者
		 * 需要实现onGetPoiDetailResult(PoiDetailResult result)和 onGetPoiResult(PoiResult result)方法
		 * */
		mPoiSearch.setOnGetPoiSearchResultListener(this);
		
		/**
		 * public static ShareUrlSearch newInstance()
		 * 获取分享URL检索实例
		 * @return 分享URL检索实例
		 * */
		mShareUrlSearch = ShareUrlSearch.newInstance();
		
		/**
		 * public void setOnGetShareUrlResultListener(OnGetShareUrlResultListener listener)
		 * 设置共享URL结果监听
		 * @return listener - 享URL结果监听者
		 * 需要实现的方法: 	
		 * onGetLocationShareUrlResult(ShareUrlResult result)
		 * onGetPoiDetailShareUrlResult(ShareUrlResult result)
		 * */
		mShareUrlSearch.setOnGetShareUrlResultListener(this);
		
		/**
		 * public static GeoCoder newInstance()
		 * 新建地理编码查询
		 * @return 地理编码查询对象
		 * */
		mGeoCoder = GeoCoder.newInstance();
		
		/**
		 * public void setOnGetGeoCodeResultListener(OnGetGeoCoderResultListener listener)
		 * 设置查询结果监听者
		 * @param listener - 监听者
		 * 
		 * 需要实现的方法: 	
		 * onGetGeoCodeResult(GeoCodeResult result)
		 * onGetReverseGeoCodeResult(ReverseGeoCodeResult result)
		 * */
		mGeoCoder.setOnGetGeoCodeResultListener(this);
	}

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

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

	@Override
	protected void onDestroy() {
		mMapView.onDestroy();
		
		/**
		 *public void destroy()
		 *释放检索对象 
		 * */
		mPoiSearch.destroy();
		
		/**
		 * public void destroy()
		 * 释放对象资源
		 * */
		mShareUrlSearch.destroy();
		
		super.onDestroy();
	}
	
	/**
	 * poi搜索结果分享按钮点击事件
	 * */
	public void sharePoi(View view) {
		/**
		 * public boolean searchInCity(PoiCitySearchOption option)
		 * 城市内检索
		 * @param option - 请求参数
		 * @return 成功发起检索返回true , 失败返回false
		 * 
		 * PoiCitySearchOption:
		 * poi城市内检索参数
		 * 
		 * public PoiCitySearchOption city(java.lang.String city)
		 * 检索城市
		 * @param city - 检索城市
		 * @return 该poi城市内检索参数对象
		 * 
		 * public PoiCitySearchOption keyword(java.lang.String key)
		 * 搜索关键字
		 * @param key - 搜索关键字
		 * @return 该poi城市内检索参数对象
		 * */
		mPoiSearch.searchInCity(
				(new PoiCitySearchOption()).city(mCity)
				.keyword(searchKey)
				);
		
		Toast.makeText(this, "在" + mCity + "搜索 " + searchKey,Toast.LENGTH_SHORT).show();
	}
	
	/**
	 * 反响地理编码分享按钮点击事件
	 * */
	public void shareAddr(View view) {
		// 发起反地理编码请求
		/**
		 * public boolean reverseGeoCode(ReverseGeoCodeOption option)
		 * @param option - 请求参数
		 * @return 成功发起检索返回true , 失败返回false
		 * 
		 * ReverseGeoCodeOption:
		 * 反地理编码请求参数
		 * 
		 * public ReverseGeoCodeOption location(LatLng location)
		 * 设置反地理编码位置坐标
		 * @param location - 位置坐标
		 * @return 该反地理编码请求参数对象
		 * */
		mGeoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(mPoint));
		
		Toast.makeText(
				this,
				String.format("搜索位置: %f,%f", mPoint.latitude, mPoint.longitude),
				Toast.LENGTH_SHORT).show();
	}
	
	OnGetPoiSearchResultListener//
	/**
	 * void onGetPoiResult(PoiResult result)
	 * poi 查询结果回调
	 * @param result - poi查询结果
	 * 
	 * public class PoiResult extends SearchResult:
	 * poi搜索结果
	 * */
	@Override
	public void onGetPoiResult(PoiResult result) {
		/**
		 * public SearchResult.ERRORNO error :
		 * 检索结果状态码, 各状态值见SearchResult.ERRORNO
		 * NO_ERROR:检索结果正常返回
		 * */
		if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
			Toast.makeText(ShareDemoActivity.this, "抱歉,未找到结果",Toast.LENGTH_LONG).show();
			return;
		}
		
		/**
		 *public final void clear()
		 *清空地图所有的 Overlay 覆盖物以及 InfoWindow 
		 * */
		mBaiduMap.clear();
		
		PoiShareOverlay poiOverlay = new PoiShareOverlay(mBaiduMap);
		
		/**
		 * public final void setOnMarkerClickListener(BaiduMap.OnMarkerClickListener listener)
		 * 设置地图 Marker 覆盖物点击事件监听者,自3.4.0版本起可设置多个监听对象,
		 * 停止监听时调用removeMarkerClickListener移除监听对象 
		 * 开发者注意根据OnMarkerClickListener的参数Marker来判断响应某个对象的点击事件
		 * */		
		mBaiduMap.setOnMarkerClickListener(poiOverlay);
		
		/**
		 * public void setData(PoiResult poiResult)
		 * 设置POI数据
		 * @param poiResult - 设置POI数据
		 * */
		poiOverlay.setData(result);
		
		/**
		 * public class PoiOverlay extends OverlayManager
		 * OverlayManager:
		 * public final void addToMap()
		 * 将所有Overlay 添加到地图上
		 * */
		poiOverlay.addToMap();
		
		/**
		 * OverlayManager:
		 * public void zoomToSpan()
		 * 缩放地图,使所有Overlay都在合适的视野内 
		 * 注: 该方法只对Marker类型的overlay有效 
		 * */
		poiOverlay.zoomToSpan();
	}
	
	/**
	 * void onGetPoiDetailResult(PoiDetailResult result)
	 * poi 详情查询结果回调
	 * @param result - poi详情查询结果
	 * 
	 * PoiDetailResult:详情检索结果
	 * */
	@Override
	public void onGetPoiDetailResult(PoiDetailResult result) {

	}
	
	OnGetShareUrlResultListener
	/**
	 * void onGetPoiDetailShareUrlResult(ShareUrlResult result)
	 * poi详情分享URL结果回调
	 * @return result - poi详情分享URL结果
	 * 
	 * public class ShareUrlResult extends SearchResult:
	 * 共享URL结果 (短串)
	 * */
	@Override
	public void onGetPoiDetailShareUrlResult(ShareUrlResult result) {

		// 分享短串结果
		Intent it = new Intent(Intent.ACTION_SEND);
		it.putExtra(Intent.EXTRA_TEXT, "您的朋友通过百度地图SDK与您分享一个位置: " + currentAddr
							+ " -- " + result.getUrl());
		it.setType("text/plain");
		startActivity(Intent.createChooser(it, "将短串分享到"));
	}
	
	/**
	 * void onGetLocationShareUrlResult(ShareUrlResult result)
	 * 位置分享URL结果回调
	 * @return result - 位置分享URL结果
	 * */
	@Override
	public void onGetLocationShareUrlResult(ShareUrlResult result) {

		// 分享短串结果
		Intent it = new Intent(Intent.ACTION_SEND);
		it.putExtra(Intent.EXTRA_TEXT, "您的朋友通过百度地图SDK与您分享一个位置: " + currentAddr
				+ " -- " + result.getUrl());
		it.setType("text/plain");
		startActivity(Intent.createChooser(it, "将短串分享到"));

	}

	OnGetGeoCoderResultListener
	/**
	 * void onGetGeoCodeResult(GeoCodeResult result)
	 * 地理编码查询结果回调函数
	 * @param result - 地理编码查询结果
	 * 
	 * public class GeoCodeResult extends SearchResult:
	 * 地理编码结果
	 * */
	@Override
	public void onGetGeoCodeResult(GeoCodeResult result) {

	}
	
	/**
	 * void onGetReverseGeoCodeResult(ReverseGeoCodeResult result)
	 * 反地理编码查询结果回调函数
	 * @param result - 反地理编码查询结果
	 * 
	 * public class ReverseGeoCodeResult extends SearchResult:
	 * 反 Geo Code 结果
	 * */
	@Override
	public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
		if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
			Toast.makeText(ShareDemoActivity.this, "抱歉,未找到结果",
					Toast.LENGTH_LONG).show();
			return;
		}
		mBaiduMap.clear();
		/**
		 * 需要实现BaiduMap.OnMarkerClickListener接口的 	onMarkerClick(Marker marker)方法
		 * */
		mBaiduMap.setOnMarkerClickListener(this);
		
		mAddrMarker = (Marker) mBaiduMap.addOverlay(new MarkerOptions()
				.icon(BitmapDescriptorFactory
						.fromResource(R.drawable.icon_marka))
				.title(result.getAddress()).position(result.getLocation()));

	}

	//OnMarkerClickListener///
	/**
	 * boolean onMarkerClick(Marker marker)
	 * 地图 Marker 覆盖物点击事件监听函数,开发者注意根据OnMarkerClickListener
	 * 的参数Marker来判断响应某个对象的点击事件
	 * @param marker - 被点击的 marker
	 * */
	@Override
	public boolean onMarkerClick(Marker marker) {
		if (marker == mAddrMarker) {
			/**
			 * public boolean requestLocationShareUrl(LocationShareURLOption option)
			 * 请求位置信息分享URL
			 * @param option - 请求参数
			 * @return 成功发起检索返回true , 失败返回false
			 * */
			
			/**
			 * LocationShareURLOption:位置信息分享URL请求参数
			 * 
			 * public LocationShareURLOption location(LatLng location)
			 * 共享点位置
			 * @param location - 共享点位置
			 * @return 该LocationShareURLOption 对象
			 * 
			 * Marker:public LatLng getPosition()
			 * 获取 Marker 覆盖物的位置坐标
			 * @return Marker 覆盖物的位置坐标
			 * 
			 * public LocationShareURLOption snippet(java.lang.String snippet)
			 * 通过短URL调起客户端时作为附加信息显示在名称下面
			 * @param snippet - 附加信息
			 * @return 该LocationShareURLOption 对象
			 * 
			 * public LocationShareURLOption name(java.lang.String name)
			 * 共享点名称
			 * @param name - 共享点名称
			 * @return 该LocationShareURLOption 对象
			 * 
			 * Marker:public java.lang.String getTitle()
			 * 获取 Marker 覆盖物的标题
			 * @return Marker 覆盖物的标题
			 * */
			mShareUrlSearch.requestLocationShareUrl(
							new LocationShareURLOption()
							.location(marker.getPosition())
							.snippet("测试分享点")
							.name(marker.getTitle())
							);
		}
		return true;
	}
	//

	/**
	 * 使用PoiOverlay 展示poi点,在poi被点击时发起短串请求.
	 * 注意:类 PoiOverlay实现了BaiduMap.OnMarkerClickListener接口
	 */
	private class PoiShareOverlay extends PoiOverlay {

		public PoiShareOverlay(BaiduMap baiduMap) {
			super(baiduMap);
		}
		
		/**
		 * public boolean onPoiClick(int i)
		 * 覆写此方法以改变默认点击行为
		 * @param i - 被点击的poi在 PoiResult.getAllPoi() 中的索引
		 * */
		@Override
		public boolean onPoiClick(int i) {
			/**
			 * public PoiResult getPoiResult():获取该 PoiOverlay 的 poi数据
			 * 
			 * public java.util.List<PoiInfo> getAllPoi()
			 * 获取Poi检索结果
			 * @return Poi检索结果
			 * 
			 * */
			PoiInfo info = getPoiResult().getAllPoi().get(i);
			
			/**
			 * public java.lang.String address:
			 * poi地址信息
			 * */
			currentAddr = info.address;
			
			/**
			 * public boolean requestPoiDetailShareUrl(PoiDetailShareURLOption option)
			 * 请求poi详情分享URL
			 * @param option - 请求参数
			 * @return 成功发起检索返回true , 失败返回false
			 * 
			 * PoiDetailShareURLOption:
			 * poi详情分享URL请求参数
			 * 
			 * public PoiDetailShareURLOption poiUid(java.lang.String uid)
			 * 设置欲分享的poi的uid
			 * @param uid
			 * 
			 * public java.lang.String uid
			 * poi id 如果为isPano为true,可用此参数 调用街景组件PanoramaService类的
			 * requestPanoramaWithPoiUId方法检索街景数据
			 * */
			mShareUrlSearch.requestPoiDetailShareUrl(new PoiDetailShareURLOption().poiUid(info.uid));
			return true;
		}
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值