工作的时候几乎很多个项目都会用到这个百度地图的定位,位置显示,等等的一些功能,本文呢就写一个百度地图简单的定位和地图点击选点,话不多说,直接上图,类似于做的这个效果
就是你点击地图上任何一点,都会得到一个位置信息,包括经纬度
下面呢就来一步步实现这个功能
1.首先你得先申请一个百度地图的APPkey这里是地址百度地图开发者注册,这你要导入百度地图官网的SDK,
这里是链接店址百度sdk下载
2.一切准备妥当了之后下面开始撸代码
首先在androidManifest.xml中配置如下代码:
<!-- 百度地图开始 -->
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote">
<intent-filter>
<action android:name="com.baidu.location.service_v2.2"></action>
</intent-filter>
</service>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="**********************" />//这里填写你申请下来的appkey
<!-- 百度地图结束 -->
3.接下来就是布局文件了.其中一些style和资源文件我这里就不码出来了,每个人每个人要的效果不同
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/hh_fi_fi"
android:background="@color/white"
android:orientation="vertical"
>
<LinearLayout
android:layout_height="48dp"
style="@style/hh_fi_wr"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_adress_top_back"
style="@style/hh_wr_wr"
android:padding="10dp"
android:src="@drawable/top_back_acc"/>
<EditText
android:id="@+id/et_adress_search_content"
style="@style/text_black_14_w_w_w"
android:background="@color/white"
android:hint="@string/input_key"
android:maxLength="20"
android:padding="10dp"
android:singleLine="true"
android:textColorHint="@color/gray_text"
/>
<ImageView
android:id="@+id/img_adress_search"
style="@style/hh_wr_wr"
android:padding="10dp"
android:src="@drawable/search_adress"/>
</LinearLayout>
<FrameLayout
style="@style/hh_fi_fi">
<com.baidu.mapapi.map.MapView
android:id="@+id/map_view"
style="@style/hh_fi_fi">
</com.baidu.mapapi.map.MapView>
<EditText
android:id="@+id/et_adress_name"
style="@style/text_black_14_f_w"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:background="@color/white"
android:drawableLeft="@drawable/map_adress"
android:drawablePadding="10dp"
android:padding="10dp"
android:textColor="@color/main_color"
/>
<TextView
android:id="@+id/tv_sure"
style="@style/tv_submit"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="@color/white"
android:drawablePadding="10dp"
android:gravity="center"
android:text="@string/sure"
android:textColor="@color/main_color"
/>
</FrameLayout>
</LinearLayout>
4.接下来就算java代码了
public class UserAdressSelectActivity extends HHBaseActivity implements BaiduMap.OnMapClickListener, View.OnClickListener {
private static final int GET_ADRESS_SUCCESS = 0;
private static final int GET_ADRESS = 2;
private ImageView topBackImageView;//头部返回
private EditText searchEditText;//搜索内容
private ImageView searchImageView;//搜索
private MapView mapView;
private EditText adressEditText;//地址
private TextView sureTextView;//确定
//地图
private BaiduMap mBaiduMap;
private LocationClient mLocClient;
private LatLng latLng;
private MyLocationListenner myListener = new MyLocationListenner();
private MapStatus mMapStatus;
private GeoCoder coder = null;
private String addr;
private String la;
private String lo;
private String provinces = "河南省";
@Override
public View initView() {
View view = View.inflate(getPageContext(), R.layout.activity_adress_select, null);
topBackImageView = getViewByID(view, R.id.img_adress_top_back);
searchEditText = getViewByID(view, R.id.et_adress_search_content);
searchImageView = getViewByID(view, R.id.img_adress_search);
mapView = getViewByID(view, R.id.map_view);
adressEditText = getViewByID(view, R.id.et_adress_name);
sureTextView = getViewByID(view, R.id.tv_sure);
return view;
}
@Override
public void initValues() {
SDKInitializer.initialize(getApplicationContext());//是程序不崩溃的作用
la = getIntent().getStringExtra("la");
lo = getIntent().getStringExtra("lo");
mBaiduMap = mapView.getMap();
int count = mapView.getChildCount();
for (int i = 0; i < count; i++) {
View child = mapView.getChildAt(i);
if (child instanceof ZoomControls || child instanceof ImageView) {
child.setVisibility(View.INVISIBLE);
}
}
if (!TextUtils.isEmpty(la) || !TextUtils.isEmpty(lo)) {
latLng = new LatLng(TurnsUtils.getDouble(la, 0), TurnsUtils.getDouble(lo, 0));
}
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
mMapStatus = new MapStatus.Builder().zoom(15).build();//设置地图缩放级别
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(mMapStatus));
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
// 定位初始化
mLocClient = new LocationClient(getPageContext());
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(0);
option.setIsNeedAddress(true);
mLocClient.setLocOption(option);
mLocClient.start();
coder = GeoCoder.newInstance();
}
@Override
public void initListeners() {
sureTextView.setOnClickListener(this);
topBackImageView.setOnClickListener(this);
searchImageView.setOnClickListener(this);
mBaiduMap.setOnMapClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.img_adress_top_back://头部返回
finish();
break;
case R.id.img_adress_search://搜索
if (!TextUtils.isEmpty(searchEditText.getText().toString().trim())) {
// searchAdress();
Intent intent=new Intent(getPageContext(),MapPoiListActivity.class);
intent.putExtra("adress",searchEditText.getText().toString().trim());
startActivityForResult(intent,GET_ADRESS);
}else {
HHTipUtils.getInstance().showToast(getPageContext(),R.string.input_search_content);
}
break;
case R.id.tv_sure://确定
Intent intent = new Intent();
intent.putExtra("adress", adressEditText.getText().toString().trim());
intent.putExtra("la", la);
intent.putExtra("lo", lo);
setResult(RESULT_OK, intent);
finish();
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK){
if (requestCode==GET_ADRESS){
if (data==null){
return;
}
la=data.getDoubleExtra("la",0)+"";
lo=data.getDoubleExtra("lo",0)+"";
HHLog.i("LYB","la=="+la);
HHLog.i("LYB","lo=="+lo);
latLng = new LatLng(data.getDoubleExtra("la",0), data.getDoubleExtra("lo",0));
mapMoveCenter(latLng);
addr=data.getStringExtra("adress_name");
adressEditText.setText(addr);
adressEditText.setSelection(adressEditText.getText().toString().trim().length());
}
}
}
@Override
public void onMapClick(LatLng arg0) {
latLng = arg0;
// 保持一个点
HHTipUtils.getInstance().showProgressDialog(getPageContext(), R.string.watting);
coder.reverseGeoCode(new ReverseGeoCodeOption().location(arg0));
}
/**
* 将地图移动到中心点
*
* @param arg0
*/
private void mapMoveCenter(LatLng arg0) {
mBaiduMap.clear();
MapStatus mMapStatus = new MapStatus.Builder()
.target(arg0)
.zoom(18)
.build();
//定义MapStatusUpdate对象,以便描述地图状态将要发生的变化
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
//改变地图状态
mBaiduMap.animateMapStatus(mMapStatusUpdate);
//定义MapStatusUpdate对象,以便描述地图状态将要发生的变化
//改变地图状态
mBaiduMap.setMapStatus(mMapStatusUpdate);
// MapStatusUpdate state = MapStatusUpdateFactory.zoomBy(4);
// mBaiduMap.animateMapStatus(state);
BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.map_my_location);
OverlayOptions option = new MarkerOptions().position(arg0).icon(mCurrentMarker);
// 在地图上添加Marker,并显示
mBaiduMap.addOverlay(option);
// 获取位置名称
}
@Override
public boolean onMapPoiClick(MapPoi mapPoi) {
return false;
}
@Override
protected void onDestroy() {
super.onDestroy();
// 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
if (mapView != null) {
mapView.onDestroy();
}
}
@Override
protected void onResume() {
super.onResume();
// 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
if (mapView != null) {
mapView.onResume();
}
}
@Override
protected void onPause() {
super.onPause();
// 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
if (mapView != null) {
mapView.onPause();
}
}
@Override
public void processHandlerMsg(Message msg) {
HHTipUtils.getInstance().dismissProgressDialog();
switch (msg.what) {
case GET_ADRESS_SUCCESS:
adressEditText.setText(addr);
break;
default:
break;
}
}
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mapView == null) {
return;
}
// 开始移动百度地图的定位地点到中心位置
if (latLng == null) {
latLng = new LatLng(location.getLatitude(), location.getLongitude());
}
MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(latLng);//地图中心点
MapStatusUpdate state = MapStatusUpdateFactory.zoomBy(4);//缩放比例
BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.map_my_location);
OverlayOptions option = new MarkerOptions().position(latLng).icon(mCurrentMarker);
coder.reverseGeoCode(new ReverseGeoCodeOption().location(latLng));
// 在地图上添加Marker,并显示
mBaiduMap.addOverlay(option);
mBaiduMap.setMapStatus(update);
mBaiduMap.animateMapStatus(state);
sendHandlerMessage(GET_ADRESS_SUCCESS);
}
@Override
public void onConnectHotSpotMessage(String arg0, int arg1) {
// TODO Auto-generated method stub
}
}
}
具体的就是如果你需要刚进入地图的时候显示到具体某个地方的时候,你可以传个经纬度,如果不传的话默认为当前所在位置,好了,一个简单的地图定位选点就实现了,欢迎提出建议,不懂的可以留言