一、Android studio环境搭建地址:
1、http://lbsyun.baidu.com/index.php?title=androidsdk/guide/create-project/androidstudio
二、复制粘贴:
1、布局:
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"/>
public class MainActivity extends AppCompatActivity {
private static final String TAG="MainActivity";
SDKReceiver mReceiver;
MapView mapView;
private BaiduMap baiduMap = null;
LatLng point;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initFilter();
initMap();
}
private void initView(){
mapView=findViewById(R.id.bmapView);
if (baiduMap == null) {
baiduMap = mapView.getMap();
}
}
private void initMap() {
BaiduMap.OnMapClickListener listener = new BaiduMap.OnMapClickListener() {
/**
* 地图单击事件回调函数
*
* @param point 点击的地理坐标
*/
@Override
public void onMapClick(LatLng point) {
Log.e(TAG, "onMapClick latitude: "+point.latitude );//经度
Log.e(TAG, "onMapClick longitude: "+point.longitude );//纬度
setMarkPoint(point.latitude,point.longitude);
}
/**
* 地图内 Poi 单击事件回调函数
*
* @param mapPoi 点击的 poi 信息
*/
@Override
public void onMapPoiClick(MapPoi mapPoi) {
Log.e(TAG, "onMapPoiClick getName: "+mapPoi.getName() );
Log.e(TAG, "onMapPoiClick getUid: "+mapPoi.getUid() );
Log.e(TAG, "onMapPoiClick getPosition: "+mapPoi.getPosition() );
}
};//点击获取经纬度;
baiduMap.setOnMapClickListener(listener);
baiduMap.setMyLocationEnabled(true);
}
private void setMarkPoint(double jingdu,double weidu) {
//定义Maker坐标点
baiduMap.clear();
point = new LatLng(jingdu, weidu);
//构建Marker图标
BitmapDescriptor bitmap = BitmapDescriptorFactory
.fromResource(R.mipmap.baiduselect_icon);
//构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions()
.position(point)
.icon(bitmap);
//在地图上添加Marker,并显示
baiduMap.addOverlay(option);
}
private void initFilter() {
IntentFilter iFilter = new IntentFilter();
iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);
iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_OK);
mReceiver = new SDKReceiver();
registerReceiver(mReceiver, iFilter);
}
}
/**-------------------------------高德地图---------------------*/
private void initLocal() {
/**
* 初始化定位并获取回调
*/
//初始化定位
mLocationClient = new AMapLocationClient(this);
//设置定位回调监听
mLocationClient.setLocationListener(mLocationListener);
//初始化定位参数
AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
//设置定位模式为Hight_Accuracy高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置是否返回地址信息(默认返回地址信息)
mLocationOption.setNeedAddress(true);
//设置是否只定位一次,默认为false
mLocationOption.setOnceLocation(false);
//设置是否强制刷新WIFI,默认为强制刷新
mLocationOption.setWifiActiveScan(true);
//设置是否允许模拟位置,默认为false,不允许模拟位置
mLocationOption.setMockEnable(false);
//设置定位间隔,单位毫秒,默认为2000ms
mLocationOption.setInterval(2000);
//给定位客户端对象设置定位参数
mLocationClient.setLocationOption(mLocationOption);
//启动定位
//启动定位
mLocationClient.startLocation();
}
private AMapLocationClient mLocationClient = null;//高德定位
private double currentLat;//获取纬度
private double currentLon;//获取经度
public AMapLocationListener mLocationListener = new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation amapLocation) {
// 从这里开始就会持续回调
if (amapLocation != null) {
if (amapLocation.getErrorCode() == 0) {
//定位成功回调信息,设置相关消息
amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
currentLat = amapLocation.getLatitude();//获取纬度
currentLon = amapLocation.getLongitude();//获取经度
// currentLatLng = new LatLng(currentLat, currentLon);
amapLocation.getAccuracy();//获取精度信息
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date date = new Date(amapLocation.getTime());
// df.format(date);//定位时间
// Log.e(TAG, "onLocationChanged 时间: "+df.format(date) );
Log.e(TAG, "currentLat : " + currentLat + " currentLon : " + currentLon);
City = amapLocation.getCity() ;
Province=amapLocation.getProvince();
if (!isSearch && City != null) {
getCityWeather(City);
isSearch = true;
}
} else {
//显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
Log.e("AmapError", "location Error, ErrCode:"
+ amapLocation.getErrorCode() + ", errInfo:"
+ amapLocation.getErrorInfo());
}
}
}
};
private void getCityWeather(String city) {
Log.e(TAG, "getCityWeather: ");
//检索参数为城市和天气类型,实况天气为WEATHER_TYPE_LIVE、天气预报为WEATHER_TYPE_FORECAST
WeatherSearchQuery mquery = new WeatherSearchQuery(city, WeatherSearchQuery.WEATHER_TYPE_LIVE);
WeatherSearch mweathersearch = new WeatherSearch(this);
mweathersearch.setOnWeatherSearchListener(new WeatherSearch.OnWeatherSearchListener() {
@Override
public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int i) {
Log.e(TAG, "onWeatherLiveSearched: " + localWeatherLiveResult.toString());
Weather=localWeatherLiveResult.getLiveResult().getWeather();
WeatherTemp=localWeatherLiveResult.getLiveResult().getTemperature();
handler.sendEmptyMessage(initWeatherType);
}
@Override
public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int i) {
Log.e(TAG, "onWeatherForecastSearched: " + localWeatherForecastResult.toString());
}
});
mweathersearch.setQuery(mquery);
cachedThreadPool.execute(new Runnable() {
@Override
public void run() {
mweathersearch.searchWeatherAsyn(); //异步搜索
handler.sendEmptyMessage(initCityType);
}
});
}