百度地图初学者
1.http://lbsyun.baidu.com/sdk/download上面下载所需要的sdk
2.key值得申请,
3.根据开发文档一步一步的完成各种的需求
4.代码的实现(下面试一个类里面的)可以直接使用
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Point;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
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.BaiduMap.OnMyLocationClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.InfoWindow.OnInfoWindowClickListener;
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.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.cserver.saas.activity.R;
import com.cserver.saas.androidoa.base.BaseActivity;
/**
* 地图
*
* @author Administrator
*
*/
public class FieldInspecMapActivity extends BaseActivity {
private static final String TAG = "FieldInspecMapActivity";
/**
* MapView
*/
public MapView mMapView = null;
/**
* LocationClient类
*/
public LocationClient mLocationClient = null;
/**
* 位置监听
*/
public BDLocationListener myListener = new MyLocationListener();
/**
* 百度地图
*/
public BaiduMap mBaiduMap;
/**
* 是否第一次加载
*/
public boolean isFirstLoc = true;
/**
* 地理编码检索实例
*/
/**
* 地球坐标
*/
LatLng ll;
/**
* 目的地(当前所在位置)
*/
String destination;
/**
* 纬度
*/
String latitude;
/**
* 经度
*/
String longitude;
/**
* 屏幕宽
*/
private int screenWidth;
/**
* 屏幕高
*/
int screenHeight;
/**
* 返回按钮
*/
private Button backBtn;
SharedPreferences setting;
/**
* 用户ID
*/
String userIds = "";
String userId = "";
Editor editor;
private LatLng latLng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//初始化sdk
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_attendancemap);
Log.e(TAG, " 进入FieldInspecMapActivity ");
setting = getSharedPreferences("oa", Activity.MODE_PRIVATE);
editor = setting.edit();
userIds = setting.getString("fieldInspectUserId", "");
userId = setting.getString("userId", "");
Log.e(TAG, "userIds " + userIds);// 组用户的userId是fieldInspectUserId
Log.e(TAG, "userId " + userId);
editor.commit();
backBtn = (Button) findViewById(R.id.backBtn);
backBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
finish();
}
});
//地图的初始化
initMap();
}
private void initMap() {
mMapView = (MapView) findViewById(R.id.bmapView);
mMapView.setClickable(true);
// 删除比例尺控件
mMapView.removeViewAt(3);
// 删除缩放控件
mMapView.removeViewAt(2);
// 屏幕宽(像素,如:480px)
screenWidth = getWindowManager().getDefaultDisplay().getWidth();
// 屏幕高(像素,如:800px)
screenHeight = getWindowManager().getDefaultDisplay().getHeight();
mBaiduMap = mMapView.getMap();
//初始化位置的确定
LatLng cenpt = new LatLng(33.242544, 108.808425);
//定义地图状态
MapStatus mMapStatus = new MapStatus.Builder()
.target(cenpt)
.zoom(13)
.build();
//定义MapStatusUpdate对象,以便描述地图状态将要发生的变化
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
//改变地图状态
mBaiduMap.setMapStatus(mMapStatusUpdate);
//下面是3.0之前的初始化位置方法,这块没有用
/*
// 获取地图控制器
mMapController = mMapView.getController();
*//**
* 设置地图是否响应点击事件 .
*//*
mMapController.enableClick(true);
*//**
* 设置地图缩放级别
*//*
mMapController.setZoom(12);
*//**
* 将地图移动至指定点
* 使用百度经纬度坐标,可以通过http://api.map.baidu.com/lbsapi/getpoint/index.html查询地理坐标
* 如果需要在百度地图上显示使用其他坐标系统的位置,请发邮件至mapapi@baidu.com申请坐标转换接口
*/
/* GeoPoint p ;
double cLat = 39.945 ;
double cLon = 116.404 ;
Intent intent = getIntent();
if ( intent.hasExtra("x") && intent.hasExtra("y") ){
//当用intent参数时,设置中心点为指定点
Bundle b = intent.getExtras();
p = new GeoPoint(b.getInt("y"), b.getInt("x"));
}else{
//设置中心点为天安门
p = new GeoPoint((int)(cLat * 1E6), (int)(cLon * 1E6));
}
mMapController.setCenter(p);*/
//ll = new LatLng(34.196421, 108.868445);
//mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(ll));
////缩放地图显示级别 默认级别:3-19
//mBaiduMap.animateMapStatus(MapStatusUpdateFactory.zoomTo(16));
//获取地图坐标
getLocationClient();
//监听回调
LocationListener();
}
/*public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null) {
GeoPoint geoPoint = new GeoPoint(
(int) (location.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6));
mMapView.getController().animateTo(geoPoint);
mMapController = mMapView.getController();*/
private void getLocationClient() {
mLocationClient = new LocationClient(this); // 声明LocationClient类
// 定位之前设置参数
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式
option.setOpenGps(true);// 打开gps
option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
option.setScanSpan(1000);// 设置发起定位请求的间隔时间为5000ms
option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向
mLocationClient.setLocOption(option);
//开启图层
mBaiduMap.setMyLocationEnabled(true);
//mLocationClient.start();
mLocationClient.registerLocationListener(myListener); // 注册监听函数
if (mLocationClient != null && mLocationClient.isStarted()) {
mLocationClient.requestLocation();
} else {
}
// 解析定位结果
//mLocationClient.registerLocationListener(new BDLocationListener() {
//public void onReceiveLocation(BDLocation location) {
////根据定位位置更新地图的中心点
//mBaiduMap.animateMapStatus(MapStatusUpdateFactory
// .newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
////添加定位在地图上
//mBaiduMap.setMyLocationData(new MyLocationData.Builder()
//.latitude(location.getLatitude()).longitude(location.getLongitude()).build());
//}
//});
}
private void LocationListener() {
mBaiduMap.setOnMyLocationClickListener(myLocationClickListener);
mBaiduMap.setOnMarkerClickListener(markerClickListener);
mBaiduMap.setOnMapClickListener(mapClickListener);
}
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null) {
Toast.makeText(FieldInspecMapActivity.this, "定位失败",
Toast.LENGTH_SHORT).show();
return;
}
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
sb.append("\nlatitude : ");
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
System.out.println("我的经纬度location.getLatitude()"+location.getLatitude()+"weilocation.getLongitude():"+ location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation) {
sb.append("\nspeed : ");
sb.append(location.getSpeed());
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
sb.append("\naddr : ");
sb.append(location.getAddrStr());
}
latitude = String.valueOf(location.getLatitude());
longitude = String.valueOf(location.getLongitude());
System.out.println("我的经纬度"+latitude+"wei:"+ longitude);
destination = location.getAddrStr();
if (destination != null && !"null".equals(destination)
&& !"".equals(destination)) {
} else {
//4.20 add
destination = "没有搜索到当前地址";
//destination = "";
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100)
.latitude(Double.parseDouble(latitude))
.longitude(Double.parseDouble(longitude))
.build();
mBaiduMap.setMyLocationData(locData);
// 缩放设置
if (isFirstLoc) {
isFirstLoc = false;
// 设置地图缩放比例
MapStatusUpdate ms = MapStatusUpdateFactory.zoomTo(16);
mBaiduMap.setMapStatus(ms);
}
// 更新地图位置
//ll = new LatLng(location.getLatitude(), location.getLongitude());
ll = new LatLng(Double.parseDouble(latitude),Double.parseDouble(longitude));
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
if(u!=null && mBaiduMap != null){
//mBaiduMap.animateMapStatus(u);
}
if (location.getLocType() == BDLocation.TypeOffLineLocationNetworkFail) {
Toast.makeText(FieldInspecMapActivity.this, R.string.wifi_exception,
Toast.LENGTH_SHORT).show();
} else {
setMarker(ll);
}
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
/**
* 加载Marker
* @param ll2
*/
public void setMarker(LatLng ll2) {
mBaiduMap.clear();
final LatLng latLng = ll2;
Log.e(TAG, "测试数据setMarker() " + latLng.latitude + " " + latLng.longitude);
// 上班签到
Point point = mBaiduMap.getProjection().toScreenLocation(ll2);
point.x -= 150;
point.y += 250;
// 定义Maker坐标点
final LatLng llInfo = mBaiduMap.getProjection().fromScreenLocation(
point);
// 构建Marker图标
BitmapDescriptor bitmap = BitmapDescriptorFactory
.fromResource(R.drawable.photo_now);
// 构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions().position(llInfo).icon(
bitmap);
// 在地图上添加Marker,并显示
mBaiduMap.addOverlay(option);
// 下班签退
Point point1 = mBaiduMap.getProjection().toScreenLocation(latLng);
point1.x += 150;
point1.y += 250;
final LatLng llInfo1 = mBaiduMap.getProjection().fromScreenLocation(
point1);
BitmapDescriptor bitmap1 = BitmapDescriptorFactory
.fromResource(R.drawable.video_now);
OverlayOptions option1 = new MarkerOptions().position(llInfo1).icon(
bitmap1);
mBaiduMap.addOverlay(option1);
lline.setVisibility(View.GONE);
progress.setVisibility(View.GONE);
}
/**
* 地图 Marker 覆盖物点击事件监听接口
*/
OnMarkerClickListener markerClickListener = new OnMarkerClickListener() {
public boolean onMarkerClick(Marker marker) {
final LatLng ll = marker.getPosition();
Point point = mBaiduMap.getProjection().toScreenLocation(ll);
if (point != null) {
if (((screenWidth / 2 - 150))- point.x <= 10 &&
((screenWidth / 2 - 150)) - point.x >= 0) {// 1.拍照
Intent intent = new Intent(FieldInspecMapActivity.this,
FieldInspecRegisterActivity.class);
intent.putExtra("destination", destination);
//4.20 add
if (latitude == "4.9E-324" ){
intent.putExtra("latitude", "");
}
if (longitude == "4.9E-324"){
intent.putExtra("longitude", "");
}
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
startActivity(intent);
mLocationClient.stop();
} else if (((screenWidth / 2 + 150))- point.x <= 10 &&
((screenWidth / 2 + 150)) - point.x >= 0) {// 2.视频
Intent intent = new Intent(FieldInspecMapActivity.this,
FieldInspectVideoActivity.class);
Log.e(TAG, "视频 " + destination);
intent.putExtra("destination", destination);
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
Log.e(TAG, "视频 longitude" + longitude);
startActivity(intent);
/* Toast.makeText(FieldInspecMapActivity.this, "正在开发中...", Toast.LENGTH_SHORT).show();*/
mLocationClient.stop();
}
}
return true;
}
};
/**
* 地图定位图标点击事件监听接口
*/
OnMyLocationClickListener myLocationClickListener = new OnMyLocationClickListener() {
public boolean onMyLocationClick() {
Button button = new Button(getApplicationContext());
if (destination != null && !"null".equals(destination)
&& !"".equals(destination)) {
button.setText(destination);
} else {
destination = "没有搜索到当前地址";
button.setText(destination);
}
// 定义用于显示该InfoWindow的坐标点
LatLng pt = ll;
// 创建InfoWindow的点击事件监听者
OnInfoWindowClickListener listener = new OnInfoWindowClickListener() {
public void onInfoWindowClick() {
// 添加点击后的事件响应代码
}
};
// 创建InfoWindow
InfoWindow mInfoWindow = new InfoWindow(button, pt, listener);
// 显示InfoWindow
mBaiduMap.showInfoWindow(mInfoWindow);
return true;
}
};
/**
* 地图单击事件监听接口
*/
OnMapClickListener mapClickListener = new OnMapClickListener() {
@Override
public boolean onMapPoiClick(MapPoi arg0) {
return false;
}
@Override
public void onMapClick(LatLng point) {
mBaiduMap.clear();
}
};
private ProgressBar progress;
private LinearLayout lline;
/**
*
* 功能描述: 获取当前地图中心点的纬度
*/
public double getMapCenterLat() {
return mBaiduMap.getMapStatus().target.latitude;
}
/**
*
* 功能描述: 获取当前地图中心点的经度
*
*/
public double getMapCenterLon() {
return mBaiduMap.getMapStatus().target.longitude;
}
@Override
public void registerBroadcastReceiver() {
}
@Override
public void unregisterBroadcastReceiver() {
}
@Override
protected void initComponents() {
}
@Override
protected void setAllListeners() {
}
@Override
protected void getAdapter() {
}
/*
* 打开设置网络界面
* */
public static void setNetworkMethod(final Context context){
//提示对话框
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("网络设置提示").setMessage("网络连接不可用,是否进行设置?").setPositiveButton("设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=null;
//判断手机系统的版本 即API大于10 就是3.0或以上版本
if(android.os.Build.VERSION.SDK_INT>10){
intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
}else{
intent = new Intent();
ComponentName component = new ComponentName("com.android.settings","com.android.settings.WirelessSettings");
intent.setComponent(component);
intent.setAction("android.intent.action.VIEW");
}
context.startActivity(intent);
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
@Override
protected void onDestroy() {
mLocationClient.stop();
mBaiduMap.setMyLocationEnabled(false);
super.onDestroy();
mMapView.onDestroy();
mMapView = null;
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
mLocationClient.start();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
}