百度地图
程序员文章站
2022-03-04 12:34:45
...
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.SearthAddressActivity">
<RelativeLayout
android:layout_width="match_parent"
android:background="@color/colorffffff"
android:layout_height="44dp">
<ImageView
android:id="@+id/iv_back"
android:layout_width="28dp"
android:layout_height="26dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:padding="5dp"
android:src="@mipmap/iv_back" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="标题"
android:textColor="@color/color404040"
android:textSize="@dimen/textSize17" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:orientation="horizontal"
android:background="@color/colorffffff"
android:paddingBottom="10dp"
android:gravity="center_vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:text="北京"
android:paddingRight="11dp"
android:textColor="@color/color000000"
android:textSize="@dimen/textSize14"
android:drawablePadding="4dp"
android:drawableRight="@mipmap/arrow_down"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:drawablePadding="8dp"
android:drawableLeft="@mipmap/home_two2"
android:hint="请输入您的上课地址"
android:textSize="@dimen/textSize14"
android:textColor="@color/color000000"
android:textColorHint="@color/colorbdbdbf"
android:background="@drawable/bg_gray"
android:layout_height="30dp" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="@+id/ceshiss"
android:layout_width="match_parent"
android:background="@color/color_orange"
android:layout_height="170dp">
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageView
android:id="@+id/sa_location"
android:layout_width="wrap_content"
android:src="@mipmap/icon_location"
android:layout_margin="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_height="wrap_content" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:text="附近地址"
android:textColor="@color/color666666"
android:textSize="@dimen/textSize14"
android:paddingTop="15dp"
android:paddingLeft="15dp"
android:paddingBottom="6dp"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/addreess_list"
android:background="@color/colorffffff"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
街道列表的适配器
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.administrator.yunleasepiano.R;
import java.util.ArrayList;
/**
* Created by hcq on 2017/1/9.
*
* @author h
*/
class PoiListAdapter extends BaseAdapter implements View.OnClickListener {
private onItemClickListener listener;
private ArrayList<String> datas;
private Context context;
PoiListAdapter(Context context, ArrayList<String> datas, onItemClickListener listener) {
this.listener = listener;
this.context = context;
this.datas = datas;
}
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int i) {
return datas.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = View.inflate(context, R.layout.item_meaddress, null);
holder.poiNameView = (TextView) convertView.findViewById(R.id.addresstext);
convertView.setOnClickListener(this);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.locationName = datas.get(position);
holder.poiNameView.setText(datas.get(position));
return convertView;
}
@Override
public void onClick(View view) {
ViewHolder holder = (ViewHolder) view.getTag();
listener.itemClickListener(holder.locationName);
}
interface onItemClickListener {
void itemClickListener(String name);
}
private class ViewHolder {
private TextView poiNameView;
private String locationName;
}
}
代码
package com.example.administrator.yunleasepiano.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ZoomControls;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
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.example.administrator.yunleasepiano.R;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class SearthAddressActivity extends AppCompatActivity implements View.OnClickListener {
@BindView(R.id.iv_back)
ImageView mIvBack;
@BindView(R.id.title)
TextView mTitle;
@BindView(R.id.sa_location)
ImageView mSaLocation;
private MapView mMapView;
private BaiduMap mBaiduMap;
public LocationClient mLocationClient;
public BDLocationListener myListener = new MyLocationListener();
private LatLng latLng;
private BDLocation bdLocation;
private boolean isFirstLoc = true; // 是否首次定位
private ArrayList<String> datas;
private PoiListAdapter adapter;
private GeoCoder geoCoder;
private double selectLat;
private double selectLon;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searth_address);
ButterKnife.bind(this);
mMapView = (MapView) findViewById(R.id.bmapView);
//获取地图控件引用
mBaiduMap = mMapView.getMap();
geoCoder = GeoCoder.newInstance();
listView = findViewById(R.id.addreess_list);
mIvBack.setOnClickListener(this);
mSaLocation.setOnClickListener(this);
initMap();
setCeshi();
initData();
iniView();
}
private void initData() {
datas = new ArrayList<>();
adapter = new PoiListAdapter(SearthAddressActivity.this, datas, new PoiListAdapter.onItemClickListener() {
@Override
public void itemClickListener(String name) {
Toast.makeText(SearthAddressActivity.this, "" + name, Toast.LENGTH_SHORT).show();
// setResult(200, new Intent().putExtra(RESULT_ADDRESS_STRING, name)
// .putExtra(RESULT_SELECT_LAT_DOUBLE, selectLat)
// .putExtra(RESULT_SELECT_LON_DOUBLE, selectLon));
// finish();
}
});
}
private void iniView() {
listView.setAdapter(adapter);
mBaiduMap.setMaxAndMinZoomLevel(20, 11);
MapStatus mapStatus = new MapStatus.Builder().zoom(19).build();
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
mBaiduMap.setMapStatus(mapStatusUpdate);
}
public void setCeshi() {
mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
Log.e("baidu点击了", "经度:" + latLng.longitude + "纬度:" + latLng.latitude);
setPopupTipsInfo(latLng);
}
@Override
public boolean onMapPoiClick(MapPoi mapPoi) {
return false;
}
});
mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener() {
@Override
public void onMapStatusChangeStart(MapStatus mapStatus) {
}
@Override
public void onMapStatusChangeStart(MapStatus mapStatus, int i) {
}
@Override
public void onMapStatusChange(MapStatus mapStatus) {
}
@Override
public void onMapStatusChangeFinish(MapStatus mapStatus) {
LatLng _latLng = mapStatus.target;
Log.e("baidu滑动后", "经度:" + _latLng.longitude + "纬度:" + _latLng.latitude);
setPopupTipsInfo(_latLng);
}
});
}
private void setPopupTipsInfo(LatLng latLng) {
//设置反地理编码位置坐标
ReverseGeoCodeOption option = new ReverseGeoCodeOption();
option.location(latLng);
//发起反地理编码请求
geoCoder.reverseGeoCode(option);
//为地理编码查询对象设置一个请求结果监听器
geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
@Override
public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
if (geoCodeResult == null || geoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(SearthAddressActivity.this, "cuowu", Toast.LENGTH_SHORT).show();
return;
}
LatLng lat = new LatLng(geoCodeResult.getLocation().latitude,
geoCodeResult.getLocation().longitude);
geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(lat));
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(lat);
mBaiduMap.clear();
mBaiduMap.animateMapStatus(u);
}
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
if (reverseGeoCodeResult == null || reverseGeoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(SearthAddressActivity.this, "cuowu", Toast.LENGTH_SHORT).show();
} else {
Log.e("baidulist", "" + reverseGeoCodeResult.getPoiList());
datas.clear();
List<PoiInfo> poiList = reverseGeoCodeResult.getPoiList();
selectLat = reverseGeoCodeResult.getLocation().latitude;
selectLon = reverseGeoCodeResult.getLocation().longitude;
if (poiList != null && poiList.size() > 0) {
for (int i = 0; i < poiList.size(); i++) {
datas.add(poiList.get(i).name);
}
}
Log.e("baidulist2", "" + datas);
adapter.notifyDataSetChanged();
}
}
});
}
private void initMap() {
//普通地图
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
mBaiduMap.setMyLocationEnabled(true);
//默认显示普通地图
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
//开启交通图
//mBaiduMap.setTrafficEnabled(true);
//开启热力图
//mBaiduMap.setBaiduHeatMapEnabled(true);
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类
//配置定位SDK参数
initLocation();
mLocationClient.registerLocationListener(myListener); //注册监听函数
//开启定位
mLocationClient.start();
//图片点击事件,回到定位点
mLocationClient.requestLocation();
}
//配置定位SDK参数
private void initLocation() {
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
int span = 1000;
option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
option.setOpenGps(true);//可选,默认false,设置是否使用gps
option.setLocationNotify(true);//可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果
option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation
// .getLocationDescribe里得到,结果类似于“在北京*附近”
option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
option.setIgnoreKillProcess(false);
option.setOpenGps(true); // 打开gps
//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤GPS仿真结果,默认需要
mLocationClient.setLocOption(option);
// 隐藏logo
View child = mMapView.getChildAt(1);
if (child != null && (child instanceof ImageView || child instanceof ZoomControls)) {
child.setVisibility(View.INVISIBLE);
}
//地图上比例尺
mMapView.showScaleControl(false);
// 隐藏缩放控件
mMapView.showZoomControls(false);
}
//实现BDLocationListener接口,BDLocationListener为结果监听接口,异步获取定位结果
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
latLng = new LatLng(location.getLatitude(), location.getLongitude());
// 构造定位数据
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
// 设置定位数据
mBaiduMap.setMyLocationData(locData);
// 当不需要定位图层时关闭定位图层
//mBaiduMap.setMyLocationEnabled(false);
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
setPopupTipsInfo(ll);
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(ll).zoom(18.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
if (location.getLocType() == BDLocation.TypeGpsLocation) {
// GPS定位结果
Toast.makeText(SearthAddressActivity.this, location.getAddrStr(), Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
// 网络定位结果
Toast.makeText(SearthAddressActivity.this, location.getAddrStr(), Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {
// 离线定位结果
Toast.makeText(SearthAddressActivity.this, location.getAddrStr(), Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeServerError) {
Toast.makeText(SearthAddressActivity.this, "服务器错误,请检查", Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
Toast.makeText(SearthAddressActivity.this, "网络错误,请检查", Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
Toast.makeText(SearthAddressActivity.this, "手机模式错误,请检查是否飞行", Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_back:
finish();
break;
case R.id.sa_location:
//把定位点再次显现出来
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(mapStatusUpdate);
break;
}
}
}
上一篇: 小程序开发之左滑删除页面(代码示例)
下一篇: C#利用泛型,避免子窗体重复开启