布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/all"
android:onClick="poiSearchBd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全城搜索"/>
<Button
android:id="@+id/nearby"
android:onClick="poiSearchBd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="附近搜索"/>
</LinearLayout>
<com.baidu.mapapi.map.MapView
android:id="@+id/mview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
==============================================================================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
===================================================================================
逻辑代码:
1.
package com.qianfeng.mytext01;
import java.util.List;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.InfoWindow.OnInfoWindowClickListener;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.overlayutil.PoiOverlay;
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.PoiDetailSearchOption;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private PoiSearch poiSearch;
private MapView mView;
private BaiduMap bMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//初始化地图
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mView=(MapView) findViewById(R.id.mview);
bMap=mView.getMap();
//设置地图中心为广州站 (113.2641,23.155209)
bMap.setMapStatus(MapStatusUpdateFactory.newLatLng(new LatLng(23.155209, 113.2641)));
//创建发起POI检索对象
poiSearch=PoiSearch.newInstance();
//绑定监听器用于接收搜索到的结果
poiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
//获取接收全城或者附近搜索的结果
@Override
public void onGetPoiResult(PoiResult arg0) {
// TODO Auto-generated method stub
//获取所有兴趣点个数
int totalPoiNum=arg0.getTotalPoiNum();
//获取兴趣点的页数
int totalPageNum=arg0.getTotalPageNum();
Log.v("TAG", "本次搜索到的兴趣点的个数:" + totalPoiNum +" , 兴趣点的总页数:" + totalPageNum);
//获取兴趣点对象的集合
List<PoiInfo>allPoi=arg0.getAllPoi();
//遍历获取每个兴趣点的信息
for (PoiInfo info : allPoi) {
String name=info.name;//兴趣点名称
String address=info.address;//获取兴趣点的地址
String number=info.phoneNum;//获取兴趣点电话号码
Log.v("TAG", "兴趣点名称:" + name + " , 兴趣点的地址:" + address + " , 兴趣点的电话号码:" + number);
}
//把收索到的兴趣点标注在百度地图上
MyPoiOverlay poiOverlay=new MyPoiOverlay(bMap);
//为兴趣点图层设定数据
poiOverlay.setData(arg0);
//把设定完数据的图层显示在地图上
poiOverlay.addToMap();
//设置标注点的点击事件
bMap.setOnMarkerClickListener(poiOverlay);
}
//接收某一个兴趣点的详情的结果
@Override
public void onGetPoiDetailResult(PoiDetailResult arg0) {
// TODO Auto-generated method stub
//获取某一个兴趣的详情信息
String address = arg0.getAddress();
String hour = arg0.getShopHours();
String url = arg0.getDetailUrl();
Log.v("TAG", "详情信息:address = " + address + " , hour = " + hour + " , url = " + url);
//启动界面二,在界面二中通过WebView显示详情的连接(网页)
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("URL", url);
startActivity(intent);
}
});
}
//poiOverlay 实现了ONMarkerClickListener接口
class MyPoiOverlay extends PoiOverlay{
public MyPoiOverlay(BaiduMap arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
@Override
public boolean onPoiClick(int arg0) {
// TODO Auto-generated method stub
Log.v("TAG", "点击的标注点的编号为:" + arg0);
//点击标注点时获取本图层上所绑定的兴趣点数据
PoiResult poiResult=getPoiResult();
List<PoiInfo>allPoi=poiResult.getAllPoi();
//获取当前被点击的兴趣点的信息对象
PoiInfo info=allPoi.get(arg0);
String name=info.name;
final String uid=info.uid;//uid属性是每一个兴趣点的唯一标记
Log.v("TAG", "点击的标注点的uid:" + uid);
Button btn=new Button(MainActivity.this);
btn.setText(name);
InfoWindow window=new InfoWindow(BitmapDescriptorFactory.fromView(btn), info.location, -50, new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick() {
// TODO Auto-generated method stub
//进行详情搜索
PoiDetailSearchOption opt=new PoiDetailSearchOption();
opt.poiUid(uid);
//发起详情搜索
poiSearch.searchPoiDetail(opt);
}
});
bMap.showInfoWindow(window);
return super.onPoiClick(arg0);
}
}
public void poiSearchBd(View v){
int id=v.getId();
switch (id) {
case R.id.all://全城搜索
PoiCitySearchOption cityOpt=new PoiCitySearchOption();//封装全城搜索请求所需的参数
cityOpt.city("广州");//指定搜索的城市
cityOpt.keyword("美食");//指定搜索的兴趣点的类型
cityOpt.pageCapacity(10);//指定每一页的数据容量
cityOpt.pageNum(0);//指定获取第几页的数据
poiSearch.searchInCity(cityOpt);//发起搜索
break;
case R.id.nearby://附近搜索
PoiNearbySearchOption nearbyOpt=new PoiNearbySearchOption();
nearbyOpt.keyword("电影院");
nearbyOpt.location(new LatLng(23.155209, 113.2641));//指明搜索圆点为广州火车站
nearbyOpt.pageCapacity(10);
nearbyOpt.pageNum(1);
nearbyOpt.radius(5000);//指明搜索半径(单位:米)
poiSearch.searchNearby(nearbyOpt);//发起附近搜索的请求
break;
}
}
}
2.
package com.qianfeng.mytext01;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
WebView web = (WebView) findViewById(R.id.web);
Intent intent = getIntent();
String url = intent.getStringExtra("URL");
web.loadUrl(url);
web.getSettings().setJavaScriptEnabled(true);//设置WebView支持JavaScript调用
web.setWebViewClient(new WebViewClient(){
//设置WebView自己处理网页连接,而不是启动系统浏览器显示网页
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;//返回值为true,表示让自己程序中的WebView处理网页链接
}
});
}
}