这几天在做一款与地图有关的应用,有点小头绪,分享给大家。
以下是相关代码
java类:
package com.angel.activity;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;
import com.angel.adapter.HotplaceAdapter;
import com.angel.towmctie.R;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.poi.PoiSearch;
import android.app.Activity;
import android.content.Context;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ZoomControls;
public class World extends Activity {
private MapView mMapView = null;
private EditText et_search_world;
private TextView tv_title_world;
private boolean isshow=false;
private ListView lv_hotplace_world;
private FrameLayout fl;
private ArrayList
list;
private BaiduMap mBaiduMap;
private LocationClient mLocationClient;
private MyLocationListener mLocationListener;
private boolean isFirstIn=true;
private Context context;
private Double latitude,longtitude;
private Geocoder geo;
private PoiSearch mPoiSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.worldsearch);
InitView();
InitData();
InitLocation();
/**
* 地图覆盖物点击事件
*/
mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker arg0) {
Toast.makeText(context, "点击覆盖物", 0).show();
return false;
}
});
/**
*记录每次地图点击位置
*/
mBaiduMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public boolean onMapPoiClick(MapPoi arg0) {
return false;
}
@Override
public void onMapClick(LatLng latlng) {
MyLocationData data = new MyLocationData.Builder()//
.accuracy(1000)//范围半径,单位:米
.latitude(latlng.latitude)//
.longitude(latlng.longitude).build();
mBaiduMap.setMyLocationData(data);
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latlng);
mBaiduMap.animateMapStatus(msu);
}
});
HotplaceAdapter ha=new HotplaceAdapter(World.this, list);
lv_hotplace_world.setAdapter(ha);
}
/**
* 初始化数据,无他用。仅供参考
*/
private void InitData() {
this.context=this;
list=new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add("angel");
}
}
/**
* 初始化相关定位设置
*/
private void InitLocation(){
geo = new Geocoder(this, Locale.CHINA);
mLocationClient=new LocationClient(context);
mLocationListener=new MyLocationListener();
mLocationClient.registerLocationListener(mLocationListener);
// 相关配置设置
LocationClientOption option = new LocationClientOption();
// 设置坐标类型
option.setCoorType("bd09ll");
option.setIsNeedAddress(true);
option.setOpenGps(true);
// 每隔5s请求一次,更新当前坐标
option.setScanSpan(5000);
mLocationClient.setLocOption(option);
}
/**
* 初始化View
*/
private void InitView() {
mMapView=(MapView) findViewById(R.id.mv_world);
mBaiduMap=mMapView.getMap();
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
mBaiduMap.setMapStatus(msu);
et_search_world=(EditText) findViewById(R.id.et_search_world);
tv_title_world=(TextView) findViewById(R.id.tv_title_world);
lv_hotplace_world=(ListView) findViewById(R.id.lv_hotplace_world);
fl=(FrameLayout) findViewById(R.id.fl_mapview_world);
int count = mMapView.getChildCount();
//隐藏缩放条
//View scale = null;
for (int i = 0; i < count; i++) {
View child = mMapView.getChildAt(i);
if ((child instanceof ZoomControls)||(child instanceof ImageView)) {
child.setVisibility(View.GONE);
}
}
//scale.setVisibility(View.GONE);
}
/**
* 检索
*/
private void search(String city,String type){
try {
Double x=geo.getFromLocationName(city, 1).get(0).getLatitude();
Double y=geo.getFromLocationName(city, 1).get(0).getLongitude();
LatLng mLatLng=new LatLng(x,y);
MyLocationData data = new MyLocationData.Builder()//
.accuracy(1000)//
.latitude(x)//
.longitude(y).build();
mBaiduMap.setMyLocationData(data);
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(mLatLng);
mBaiduMap.animateMapStatus(msu);
}
@Override
protected void onStart() {
super.onStart();
mBaiduMap.setMyLocationEnabled(true);
if (!mLocationClient.isStarted()) {
mLocationClient.start();
}
}
@Override
protected void onStop() {
super.onStop();
mBaiduMap.setMyLocationEnabled(false);
mLocationClient.stop();
}
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
//按钮点击事件
public void click(View v){
switch (v.getId()) {
case R.id.bt_back_world:
finish();
break;
//回到使用者当前位置
case R.id.bt_local_world:
MyLocationData data = new MyLocationData.Builder()//
.accuracy(1000)//范围半径,单位:米
.latitude(latitude)//
.longitude(longtitude).build();
mBaiduMap.setMyLocationData(data);
LatLng mlLatLng=new LatLng(latitude,longtitude);
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(mlLatLng);
mBaiduMap.animateMapStatus(msu);
break;
case R.id.bt_search_world:
if (!isshow) {
tv_title_world.setVisibility(View.GONE);
et_search_world.setVisibility(View.VISIBLE);
isshow=true;
}else {
et_search_world.setVisibility(View.GONE);
tv_title_world.setVisibility(View.VISIBLE);
isshow=false;
new Thread(new Runnable() {
@Override
public void run() {
search("北京", "美食");
}
}).start();;
}
break;
default:
break;
}
}
private class MyLocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(BDLocation location) {
latitude=location.getLatitude();
longtitude=location.getLongitude();
LatLng mlLatLng=new LatLng(location.getLatitude(), location.getLongitude());
//首次进去定位
if (isFirstIn) {
MyLocationData data = new MyLocationData.Builder()
.accuracy(1000)
.latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(data);
mLocationClient.requestLocation();
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(mlLatLng);
mBaiduMap.animateMapStatus(msu);
isFirstIn=false;
Toast.makeText(context, location.getAddrStr(), 0).show();
}
//标注地点
//构建Marker图标
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.position3);
//清除前一次定位的覆盖物
mBaiduMap.clear();
//构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions().position(mlLatLng).icon(bitmap);
//在地图上添加Marker,并显示
mBaiduMap.addOverlay(option);
}
}
}
布局文件:
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#E6E6E6"> android:layout_height="40dp" android:layout_width="match_parent" android:background="#29AAE3"> android:id="@+id/bt_back_world" android:layout_height="30dp" android:layout_width="20dp" android:gravity="center" android:onClick="click" android:layout_marginLeft="10dp" android:layout_marginTop="5dp" android:background="@drawable/fanhui"/> android:id="@+id/bt_search_world" android:layout_width="30dp" android:layout_height="30dp" android:onClick="click" android:gravity="center" android:layout_marginTop="5dp" android:layout_marginRight="10dp" android:layout_alignParentRight="true" android:background="@drawable/sousuo2"/> android:id="@+id/tv_title_world" android:layout_width="wrap_content" android:layout_height="30dp" android:textSize="16sp" android:text="世界留言板" android:gravity="center" android:layout_centerInParent="true"/> android:id="@+id/et_search_world" android:layout_width="match_parent" android:layout_height="30dp" android:textSize="14sp" android:visibility="gone" android:layout_toRightOf="@id/bt_back_world" android:layout_toLeftOf="@id/bt_search_world" android:layout_centerInParent="true" android:layout_marginRight="10dp" android:layout_marginLeft="10dp" android:paddingLeft="10dp" android:background="@drawable/baiseanniu" android:hint="搜索"/> android:id="@+id/fl_mapview_world" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3" android:layout_marginTop="5dp" > android:id="@+id/mv_world" android:layout_width="fill_parent" android:layout_height="match_parent" android:clickable="true" /> android:id="@+id/bt_local_world" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:onClick="click" android:background="@drawable/location"/> android:id="@+id/lv_hotplace_world" android:layout_width="match_parent" android:layout_height="0dp" android:background="#F7F7F7" android:layout_weight="2"> 当然,权限是不能少的