http://blog.csdn.net/yangbin0513/article/details/53380185
接着昨天写的文章...代码也是同一个项目代码
今天讲的是最近在项目中. 写的一个高德地图+百度地图合成在一个APK里面做的一个经纬度采集器
今天说的是高德地图SDK-------拖动Marker.得到变化的经纬度
先去下载JAR包
这是我加进去的JAR包
关于Maker点(标记点)
1.默认标记点
LatLng latLng = new LatLng(39.906901,116.397972);
final Marker marker = aMap.addMarker(new MarkerOptions().
position(latLng).
title("北京").
snippet("DefaultMarker"));
2.自定义标记点
markerOption = new MarkerOptions();
markerOption.position(Constants.XIAN);
markerOption.title("西安市").snippet("西安市:34.341568, 108.940174");
markerOption.draggable(true);
markerOption.icon(
BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(),
R.drawable.location_marker)));
// 将Marker设置为贴地显示,可以双指下拉看效果
markerOption.setFlat(true);
下面是标记的一些常用属性: 属性列表
名称 | 说明 |
---|---|
position | 在地图上标记位置的经纬度值。必填参数 |
title | 点标记的标题 |
snippet | 点标记的内容 |
draggable | 点标记是否可拖拽 |
visible | 点标记是否可见 |
anchor | 点标记的锚点 |
alpha | 点的透明度 |
1)、标记点击回调AMap.OnMarkerClickListener,监听标记点击事件,示例代码如下:
OnMarkerClickListener listener = new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker arg0) {
// TODO Auto-generated method stub
return false;
}
};
//绑定标注点击事件
AMap.setOnMarkerClickListener(listener);
2)、标记拖拽回调AMap.OnMarkerDragListener,监听标记拖拽事件,示例代码如下:
OnMarkerDragListener listener = new OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker arg0) {
// TODO Auto-generated method stub
}
@Override
public void onMarkerDragEnd(Marker arg0) {
// TODO Auto-generated method stub
}
@Override
public void onMarkerDrag(Marker arg0) {
// TODO Auto-generated method stub
}
};
//绑定标记拖拽事件
AMap.setOnMarkerDragListener(listener);
3)、信息窗点击事件AMap.OnInfoWindowClickListener
OnInfoWindowClickListener listener = new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker arg0) {
arg0.setTitle("infowindow clicked");
}
};
//绑定信息窗点击事件
AMap.setOnInfoWindowClickListener(listener);
/**
* 监听自定义infowindow窗口的infocontents事件回调
*/
@Override
public View getInfoContents(Marker marker) {
if (radioOption.getCheckedRadioButtonId() != R.id.custom_info_contents) {
return null;
}
View infoContent = getLayoutInflater().inflate(
R.layout.custom_info_contents, null);
render(marker, infoContent);
return infoContent;
}
/**
* 监听自定义infowindow窗口的infowindow事件回调
*/
@Override
public View getInfoWindow(Marker marker) {
if (radioOption.getCheckedRadioButtonId() != R.id.custom_info_window) {
return null;
}
View infoWindow = getLayoutInflater().inflate(
R.layout.custom_info_window, null);
render(marker, infoWindow);
return infoWindow;
}
/**
* 自定义infowinfow窗口
*/
public void render(Marker marker, View view) {
if (radioOption.getCheckedRadioButtonId() == R.id.custom_info_contents) {
((ImageView) view.findViewById(R.id.badge))
.setImageResource(R.drawable.badge_sa);
} else if (radioOption.getCheckedRadioButtonId() == R.id.custom_info_window) {
ImageView imageView = (ImageView) view.findViewById(R.id.badge);
imageView.setImageResource(R.drawable.badge_wa);
}
String title = marker.getTitle();
TextView titleUi = ((TextView) view.findViewById(R.id.title));
if (title != null) {
SpannableString titleText = new SpannableString(title);
titleText.setSpan(new ForegroundColorSpan(Color.RED), 0,
titleText.length(), 0);
titleUi.setTextSize(15);
titleUi.setText(titleText);
} else {
titleUi.setText("");
}
String snippet = marker.getSnippet();
TextView snippetUi = ((TextView) view.findViewById(R.id.snippet));
if (snippet != null) {
SpannableString snippetText = new SpannableString(snippet);
snippetText.setSpan(new ForegroundColorSpan(Color.GREEN), 0,
snippetText.length(), 0);
snippetUi.setTextSize(20);
snippetUi.setText(snippetText);
} else {
snippetUi.setText("");
}
}
现在说我写的这个, 首先看效果GIF图. 记住是长按然后拖动Marker点
上主代码
package com.meizu.yangbin;
import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.model.MyLocationStyle;
/**
* Created by yangbin on 11/22/2016.
*/
public class GaoDeMapActivity extends Activity implements LocationSource,
AMapLocationListener {
TextView mJingdu;
TextView mWeidu;
MapView mapView;
private AMap aMap;
private OnLocationChangedListener mListener;
private AMapLocationClient mlocationClient;
private AMapLocationClientOption mLocationOption;
//标识,用于判断是否只显示一次定位信息和用户重新定位
private boolean isFirstLoc = true;
MarkerOptions markerOption;
LatLng latLng;
private static final int STROKE_COLOR = Color.argb(180, 3, 145, 255);
private static final int FILL_COLOR = Color.argb(10, 0, 0, 180);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gaode);
mapView = (MapView) findViewById(R.id.map);
mWeidu = (TextView) findViewById(R.id.weidu);
mJingdu = (TextView) findViewById(R.id.jingdu);
mapView.onCreate(savedInstanceState);// 此方法必须重写
init();
}
/**
* 初始化AMap对象
*/
private void init() {
if (aMap == null) {
aMap = mapView.getMap();
setUpMap();
}
}
/**
* 设置一些amap的属性
*/
private void setUpMap() {
// 自定义系统定位蓝点
MyLocationStyle myLocationStyle = new MyLocationStyle();
// 自定义定位蓝点图标
myLocationStyle.myLocationIcon(BitmapDescriptorFactory.
fromResource(R.drawable.gps_point));
// 自定义精度范围的圆形边框颜色
myLocationStyle.strokeColor(STROKE_COLOR);
//自定义精度范围的圆形边框宽度
myLocationStyle.strokeWidth(2);
// 设置圆形的填充颜色
myLocationStyle.radiusFillColor(FILL_COLOR);
// 将自定义的 myLocationStyle 对象添加到地图上
aMap.setMyLocationStyle(myLocationStyle);
aMap.setLocationSource(this);// 设置定位监听
aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
aMap.setOnMarkerDragListener(new AMap.OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker marker) {//长按拖动开始
// ToastUtil.showLong("开始得到经纬度");
}
@Override
public void onMarkerDrag(Marker marker) {
//拖动中
mJingdu.setText(marker.getPosition().latitude + "");
mWeidu.setText(marker.getPosition().longitude + "");
}
@Override
public void onMarkerDragEnd(Marker marker) {
//拖动结束
mWeidu.setText(marker.getPosition().latitude + "");
mJingdu.setText(marker.getPosition().longitude + "");
Intent intent = new Intent(GaoDeMapActivity.this, MainActivity.class);
intent.putExtra("gaodejingdu", marker.getPosition().longitude + "");
intent.putExtra("gaodeweidu", marker.getPosition().latitude + "");
setResult(1, intent);
}
});
aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
}
/**
* 方法必须重写
*/
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
/**
* 方法必须重写
*/
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
deactivate();
}
/**
* 方法必须重写
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
/**
* 方法必须重写
*/
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
/**
* 定位成功后回调函数
*/
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (mListener != null && aMapLocation != null) {
if (aMapLocation != null
&& aMapLocation.getErrorCode() == 0) {
mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点
//定位成功回调信息,设置相关消息
// 如果不设置标志位,此时再拖动地图时,它会不断将地图移动到当前的位置
if (isFirstLoc) {
//设置缩放级别
aMap.moveCamera(CameraUpdateFactory.zoomTo(20));
//将地图移动到定位点
aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude())));
//点击定位按钮 能够将地图的中心移动到定位点
mListener.onLocationChanged(aMapLocation);
//获取定位信息
StringBuffer buffer = new StringBuffer();
addMarkerToMap(aMapLocation.getLatitude(), aMapLocation.getLongitude());
buffer.append(aMapLocation.getCountry() + ""
+ aMapLocation.getProvince() + ""
+ aMapLocation.getCity() + ""
+ aMapLocation.getProvince() + ""
+ aMapLocation.getDistrict() + ""
+ aMapLocation.getStreet() + ""
+ aMapLocation.getStreetNum());
android.widget.Toast.makeText(getApplicationContext(), buffer.toString(), android.widget.Toast.LENGTH_LONG).show();
isFirstLoc = false;
mWeidu.setText(aMapLocation.getLatitude() + "");
mJingdu.setText(aMapLocation.getLongitude() + "");
Intent intent = new Intent(GaoDeMapActivity.this, MainActivity.class);
intent.putExtra("gaodeweidu", aMapLocation.getLatitude() + "");
intent.putExtra("gaodejingdu", aMapLocation.getLongitude() + "");
setResult(1, intent);
}
} else {
String errText = "定位失败," + aMapLocation.getErrorCode() + ": " + aMapLocation.getErrorInfo();
Log.e("AmapErr", errText);
}
}
}
private void addMarkerToMap(double jingdu, double weidu) {
latLng = new LatLng(jingdu, weidu);
markerOption = new MarkerOptions();
markerOption.position(latLng);
Marker marker = aMap.addMarker(markerOption);
marker.setDraggable(true);
marker.setIcon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(), R.drawable.gps_red)))
;
marker.showInfoWindow();
marker.setRotateAngle(30);
}
/**
* 激活定位
*/
@Override
public void activate(OnLocationChangedListener listener) {
mListener = listener;
if (mlocationClient == null) {
mlocationClient = new AMapLocationClient(this);
mLocationOption = new AMapLocationClientOption();
//设置定位监听
mlocationClient.setLocationListener(this);
//设置为高精度定位模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置定位参数
mlocationClient.setLocationOption(mLocationOption);
// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
// 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
// 在定位结束后,在合适的生命周期调用onDestroy()方法
// 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
mlocationClient.startLocation();
}
}
/**
* 停止定位
*/
@Override
public void deactivate() {
mListener = null;
if (mlocationClient != null) {
mlocationClient.stopLocation();
mlocationClient.onDestroy();
}
mlocationClient = null;
}
}
android:name="com.amap.api.location.APSService"/>
代码下载地址