android百度地图定位|android百度地图定位,点击给定经纬度的某点进行弹窗

源码啊已经上传,是首次学习android百度地图的开发,有定位功能,点击弹窗功能,key的申请就不说了,直接在这里附上源码,最后面还有文件:


效果图片:
android百度地图定位|android百度地图定位,点击给定经纬度的某点进行弹窗
文章图片


MainActivity部分:
package com.yskj.mapdevelopdemo;


import java.util.List;


import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


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.InfoWindow;
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.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
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.yskj.domain.Info;


public class MainActivity extends Activity {
/**
* 一个显示地图(数据来自MapABC地图服务)的视图
*/
private MapView mapView = null;


private BaiduMap baiduMap = null;


/**
* 详细地址,周边,到这里去
*/
private TextView locatePosition, nearly, goingTo;


/**
* 点击获取的地址
*/
private String pointAddress;


/**
* 定义弹窗的布局
*/
private RelativeLayout MarkerDetailsInfo;


/**
* 当前的精度
*/
private float mCurrentAccracy;


/**
* 最新一次的经纬度
*/
private double mCurrentLantitude, mCurrentLongitude;


/**
* 当前定位的模式
*/
private LocationMode mCurrentMode = null;


/**
* 定位相关声明
*/
public LocationClient locationClient = null;


/**
* 是否首次定位
*/
private boolean isFristLocation = true;


/**
* 手动定位按钮
*/
private Button mButton;


/**
* 点击弹窗
*/
private InfoWindow mInfoWindow;


/**
* 初始化全局 bitmap 信息,不用时及时 recycle
*/
private BitmapDescriptor mIconMaker;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 在使用SDK各组件之前初始化context信息,传入ApplicationContext
// 注意该方法要再setContentView方法之前实现
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);


mButton = (Button) findViewById(R.id.request);
// locatePosition = (TextView) findViewById(R.id.position_details);
// nearly = (TextView) findViewById(R.id.nearly_position);
// goingTo = (TextView) findViewById(R.id.go_to);
MarkerDetailsInfo = (RelativeLayout) findViewById(R.id.line_point_details);


mButton.setOnClickListener(listener);
// locatePosition.setOnClickListener(listener);
// nearly.setOnClickListener(listener);
// goingTo.setOnClickListener(listener);


// 获取地图控件引用
mapView = (MapView) this.findViewById(R.id.bmapView);
baiduMap = mapView.getMap();
// 获得地图的实例
mIconMaker = BitmapDescriptorFactory.fromResource(R.drawable.point_start);
MapStatusUpdate update = MapStatusUpdateFactory.zoomTo(15.0f);
baiduMap.setMapStatus(update);


// 初始化点击事件
initMapClickEvent();
// 添加位置图标
addInfosOverlay(Info.infos);
// 对添加的位置图标设定点击事件
initMarkerClickEvent();


// 去掉地图内置的缩放控件
mapView.showZoomControls(false);
// 去掉百度地图Logo
mapView.removeViewAt(1);
// 开启定位图层
baiduMap.setMyLocationEnabled(true);
// 实例化LocationClient类
locationClient = new LocationClient(getApplicationContext());
// 注册监听函数
locationClient.registerLocationListener(myListener);
// 设置定位参数
this.setLocationOption();
// 开始定位
locationClient.start();


// 设置为一般地图
baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);


// baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); // 设置为卫星地图
// baiduMap.setTrafficEnabled(true); // 开启交通图


// getPositionInfo(31.383755, 118.438321);
// getPositionInfo(31.384379, 118.437616);
// getPositionInfo(31.384017, 118.441379);


}


/**
* 给指定经纬度的地方添加图标
*
* @param latitude
* @param longitude
*/
private void getPositionInfo(double latitude, double longitude) {
LatLng pointPosition = new LatLng(latitude, longitude);
// 构建Marker图标
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.point_start);
// 构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions().position(pointPosition).icon(bitmap);
// 在地图上添加Marker,并显示
baiduMap.addOverlay(option);


}


/**
* 覆盖物的点击事件
*/
private void initMarkerClickEvent() {
// 对Marker的点击
baiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {


@Override
public boolean onMarkerClick(final Marker marker) {
// 底部弹窗的信息实体类
Info info = (Info) marker.getExtraInfo().get("info");


// 生成一个TextView用户在地图中显示InfoWindow
final TextView location = new TextView(getApplicationContext());
location.setBackgroundResource(R.drawable.location_tips);
location.setPadding(30, 20, 30, 50);
location.setText(info.getHotelAddress());
// 将marker所在的经纬度的信息转化成屏幕上的坐标
final LatLng position = marker.getPosition();
Point point = baiduMap.getProjection().toScreenLocation(position);
point.y -= 47;
// 定义用于显示该InfoWindow的坐标点
LatLng positionInfo = baiduMap.getProjection().fromScreenLocation(point);
// 创建InfoWindow , 传入 view, 地理坐标, y 轴偏移量
InfoWindow mInfoWindow = new InfoWindow(location, positionInfo, 8);


// 依据当前给出的经纬度获取该地地址信息
// GeoCoder coder = GeoCoder.newInstance();
// ReverseGeoCodeOption reverseCode = new
// ReverseGeoCodeOption();
// ReverseGeoCodeOption result =
// reverseCode.location(positionInfo);
// coder.reverseGeoCode(result);
// coder.setOnGetGeoCodeResultListener(new
// OnGetGeoCoderResultListener() {
//
// @Override
// public void onGetReverseGeoCodeResult(ReverseGeoCodeResult
// result) {
// location.setText(result.getAddress());
// }
//
// @Override
// public void onGetGeoCodeResult(GeoCodeResult result) {
//
// }
// });


// 显示InfoWindow
baiduMap.showInfoWindow(mInfoWindow);
// 设置详细信息布局为可见
MarkerDetailsInfo.setVisibility(View.VISIBLE);
// 根据商家信息为详细信息布局设置信息
popupInfo(MarkerDetailsInfo, info);
return true;
}
});


}


/**
* 根据info为布局上的控件设置信息
*
* @param markerDetailsInfo2
* @param info
*/
protected void popupInfo(RelativeLayout markerInfo, Info info) {
ViewHolder viewHolder = null;
if (markerInfo.getTag() == null) {
viewHolder = new ViewHolder();
viewHolder.hotelImg = (ImageView) findViewById(R.id.hotel_img);
// viewHolder.hotelAddress = (TextView) markerInfo.findViewById(R.id.position_details);
// viewHolder.hotelDetails = (TextView) markerInfo.findViewById(R.id.hotel_details);
// viewHolder.goHere = (TextView) markerInfo.findViewById(R.id.go_to);


markerInfo.setTag(viewHolder);
}
viewHolder = (ViewHolder) markerInfo.getTag();
viewHolder.hotelImg.setImageResource(info.getImgId());
// viewHolder.hotelAddress.setText(info.getHotelAddress());
// viewHolder.hotelDetails.setText(info.getDetails());
// viewHolder.goHere.setText(info.getGoHere());
}


/**
* 复用弹出面板mMarkerLy的控件
*
*/
private class ViewHolder {
ImageView hotelImg;
// TextView hotelAddress;
// TextView hotelDetails;
// TextView goHere;
}


/**
* 初始化图层
*/
public void addInfosOverlay(List infos) {
baiduMap.clear();
LatLng latLng = null;
OverlayOptions overlayOptions = null;
Marker marker = null;
for (Info info : infos) {
// 位置
latLng = new LatLng(info.getLatitude(), info.getLongitude());
// 图标
overlayOptions = new MarkerOptions().position(latLng).icon(mIconMaker).zIndex(5);
marker = (Marker) (baiduMap.addOverlay(overlayOptions));
Bundle bundle = new Bundle();
bundle.putSerializable("info", info);
marker.setExtraInfo(bundle);
}
// 将地图移到到最后一个经纬度位置
MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(latLng);
baiduMap.setMapStatus(update);
}


/**
* 地图平面点击事件
*/
private void initMapClickEvent() {
baiduMap.setOnMapClickListener(new OnMapClickListener() {


@Override
public boolean onMapPoiClick(MapPoi position) {
return false;
}


@Override
public void onMapClick(LatLng position) {
MarkerDetailsInfo.setVisibility(View.INVISIBLE);
baiduMap.hideInfoWindow();
}
});
}


/**
* 定位监听
*/
public BDLocationListener myListener = new BDLocationListener() {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mapView == null)
return;


MyLocationData locData = https://www.it610.com/article/new MyLocationData.Builder().accuracy(location.getRadius()).direction(100).latitude(location.getLatitude()).longitude(location.getLongitude()).build();
mCurrentAccracy = location.getRadius();
baiduMap.setMyLocationData(locData); // 设置定位数据
mCurrentLantitude = location.getLatitude();
mCurrentLongitude = location.getLongitude();


// 设置自定义图标
BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.navi_map_gps_locked);
MyLocationConfiguration config = new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker);
baiduMap.setMyLocationConfigeration(config);


if (isFristLocation) {
isFristLocation = false;
LatLng position = new LatLng(location.getLatitude(), location.getLongitude());
// 设置地图中心点以及缩放级别
MapStatusUpdate update = MapStatusUpdateFactory.newLatLngZoom(position, 16);
baiduMap.animateMapStatus(update);
}


// 获取当前经纬度的地址信息
// GeoCoder coder = GeoCoder.newInstance();
// ReverseGeoCodeOption reverseCode = new ReverseGeoCodeOption();
// ReverseGeoCodeOption result = reverseCode.location(new
// LatLng(location.getLatitude(), location.getLongitude()));
// coder.reverseGeoCode(result);
// coder.setOnGetGeoCodeResultListener(new
// OnGetGeoCoderResultListener() {
//
// @Override
// public void onGetReverseGeoCodeResult(ReverseGeoCodeResult
// result) {
// locatePosition.setText(result.getAddress());
// }
//
// @Override
// public void onGetGeoCodeResult(GeoCodeResult result) {
//
// }
// });


}
};


/**
* 设置定位参数
*/
private void setLocationOption() {
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打开GPS
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy); // 设置定位模式
option.setCoorType("bd09ll"); // 返回的定位结果是百度经纬度,默认值gcj02
option.setScanSpan(5000); // 设置发起定位请求的间隔时间为5000ms
option.setIsNeedAddress(true); // 返回的定位结果包含地址信息
option.setNeedDeviceDirect(true); // 返回的定位结果包含手机机头的方向


locationClient.setLocOption(option);
}


/**
* 点击的监听事件手动请求定位的方法
*/
private OnClickListener listener = new OnClickListener() {


@Override
public void onClick(View view) {
if (view == mButton) {
LatLng position = new LatLng(mCurrentLantitude, mCurrentLongitude);
// 设置地图中心点以及缩放级别
MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(position);
baiduMap.animateMapStatus(update);
// isRequest = true;
// if (locationClient != null && locationClient.isStarted()) {
// locationClient.requestLocation();
// } else {
// Log.d("LocSDKv5.2", "locClient is null or not started");
// }
} else if (view == nearly) {


} else if (view == goingTo) {


}
}
};


/**
* 点击获取详情
*/


// 三个状态实现地图生命周期管理
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}


@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}


@Override
protected void onDestroy() {
// 退出时销毁定位
locationClient.stop();
baiduMap.setMyLocationEnabled(false);
super.onDestroy();
mapView.onDestroy();
mapView = null;
}

}





这个Info是从网络上下载下来的:下载的出处:http://blog.csdn.net/lmj623565791/article/details/37737213
package com.yskj.domain;


import com.yskj.mapdevelopdemo.R;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class Info implements Serializable {
private static final long serialVersionUID = -758459502806858414L;
/**
* 经度
*/
private double latitude;


/**
* 纬度
*/
private double longitude;


/**
* 图片ID,真实项目中可能是图片路径
*/
private int imgId;


/**
* 酒店详细地址
*/
private String hotelAddress;


public static List infos = new ArrayList();


static {
// infos.add(new Info(31.383755, 118.438321, R.drawable.a01, "英伦贵族小旅馆", "距离209米", "1456"));
// infos.add(new Info(31.384379, 118.437616, R.drawable.a02, "沙井国际洗浴会所", "距离897米", "456"));
// infos.add(new Info(31.384017, 118.441379, R.drawable.a03, "五环服装城", "距离249米", "1456"));
// infos.add(new Info(31.384417, 118.444379, R.drawable.a04, "老米家泡馍小炒", "距离679米", "1456"));
infos.add(new Info(31.383755, 118.438321, R.drawable.a01, "英伦贵族小旅馆"));
infos.add(new Info(31.384379, 118.437616, R.drawable.a02, "沙井国际洗浴会所"));
infos.add(new Info(31.384017, 118.441379, R.drawable.a03, "五环服装城"));
infos.add(new Info(31.384417, 118.444379, R.drawable.a04, "老米家泡馍小炒"));
}


public Info() {
}


public Info(double latitude, double longitude, int imgId, String hotelAddress) {
super();
this.latitude = latitude;
this.longitude = longitude;
this.imgId = imgId;
this.hotelAddress = hotelAddress;
}


public double getLatitude() {
return latitude;
}


public void setLatitude(double latitude) {
this.latitude = latitude;
}


public double getLongitude() {
return longitude;
}


public void setLongitude(double longitude) {
this.longitude = longitude;
}


public int getImgId() {
return imgId;
}


public void setImgId(int imgId) {
this.imgId = imgId;
}


public String getHotelAddress() {
return hotelAddress;
}


public void setHotelAddress(String hotelAddress) {
this.hotelAddress = hotelAddress;
}


public static long getSerialversionuid() {
return serialVersionUID;
}


}


布局文件:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >


android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />


android:id="@+id/request"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/custom_loc" />


android:id="@+id/line_point_details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:visibility="gone"
android:background="@drawable/bg_content_background"
android:clickable="true" >


android:id="@+id/hotel_img"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:alpha="1.0"
android:clickable="true"
android:scaleType="fitXY"
android:src="https://www.it610.com/article/@drawable/a04" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/hotel_img"
android:orientation="horizontal" >

android:id="@+id/hotel_details"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center"
android:text="详情" />

android:layout_width="1dp"
android:layout_height="40dp"
android:background="@drawable/bg_vertical_line" />

android:id="@+id/go_here"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center"
android:text="到这里去" />













配置清单:



package="com.yskj.mapdevelopdemo"
android:versionCode="1"
android:versionName="1.0" >


android:minSdkVersion="8"
android:targetSdkVersion="22" />

































android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >



android:name="com.baidu.lbsapi.API_KEY"
android:value="https://www.it610.com/article/AGv4eQtPlF7qlKLn4GzQQGNZ" />


android:name=".MainActivity"
android:label="@string/app_name" >














由于上传许久都没上传上来,只有放在360云盘了
源码下载地址:http://yunpan.cn/cwXg54Lk4PHVc访问密码 cb89




【android百度地图定位|android百度地图定位,点击给定经纬度的某点进行弹窗】

    推荐阅读