`
yangs_lx
  • 浏览: 2000 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

高德地图定位,点击标注

阅读更多
package com.amap.map2d.demo.basic;

import android.app.Activity;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.AMap.OnMapClickListener;
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;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.map2d.demo.R;
import com.amap.map2d.demo.util.ToastUtil;

/**
* AMapV1地图中简单介绍OnMapClickListener,
*/

public class EventsActivity extends Activity implements OnMapClickListener,LocationSource,
AMapLocationListener,OnGeocodeSearchListener{
private AMap aMap;
private MapView mapView;
private Marker marker;
private OnLocationChangedListener mListener;
private LocationManagerProxy mAMapLocationManager;
private AMapLocation aMapLocation;
private GeocodeSearch geocoderSearch;
private String addressName;
private LatLonPoint latLonPoint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.events_activity);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);// 此方法必须重写
init();
}

/**
* 初始化AMap对象
*/
private void init() {
if (aMap == null) {
geocoderSearch = new GeocodeSearch(this);
geocoderSearch.setOnGeocodeSearchListener(this);
aMap = mapView.getMap();
setUpMap();
}
}

/**
* amap添加一些事件监听器
*/
private void setUpMap() {

aMap.setOnMapClickListener(this);// 对amap添加单击地图事件监听器
// 自定义系统定位小蓝点
MyLocationStyle myLocationStyle = new MyLocationStyle();
myLocationStyle.myLocationIcon(BitmapDescriptorFactory
.fromResource(R.drawable.location_marker));// 设置小蓝点的图标
myLocationStyle.strokeColor(Color.BLACK);// 设置圆形的边框颜色
myLocationStyle.radiusFillColor(Color.argb(100, 0, 0, 180));// 设置圆形的填充颜色
myLocationStyle.strokeWidth(1.0f);// 设置圆形的边框粗细
aMap.setMyLocationStyle(myLocationStyle);
aMap.setLocationSource(this);// 设置定位监听
aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
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 onMapClick(LatLng point) {
if (marker != null) {
marker.remove();
}
// marker = aMap.addMarker(new MarkerOptions()
// .anchor(0.5f, 0.5f).position(new LatLng(point.latitude, point.longitude))
// // 地图上的位置
// .title("aaaa")
// // 要显示的内容
// .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)).draggable(false));
// marker.showInfoWindow();
latLonPoint = new LatLonPoint(point.latitude, point.longitude);
getAddress(latLonPoint);
}

/**
* 此方法已经废弃
*/
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onGeocodeSearched(GeocodeResult result, int rCode) {
// TODO Auto-generated method stub
}

@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
// TODO Auto-generated method stub
if (rCode == 0) {
if (result != null && result.getRegeocodeAddress() != null
&& result.getRegeocodeAddress().getFormatAddress() != null) {
addressName = result.getRegeocodeAddress().getFormatAddress()+ "附近";
marker = aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
                .position(new LatLng(latLonPoint.getLatitude(), latLonPoint.getLongitude()))//地图上的位置
                .title(addressName)//要显示的内容
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                .draggable(false));
        marker.showInfoWindow();
} else {
ToastUtil.show(EventsActivity.this, R.string.no_result);
}
} else if (rCode == 27) {
ToastUtil.show(EventsActivity.this, R.string.error_network);
} else if (rCode == 32) {
ToastUtil.show(EventsActivity.this, R.string.error_key);
} else {
ToastUtil.show(EventsActivity.this,
getString(R.string.error_other) + rCode);
}
}
/**
* 响应逆地理编码
*/
public void getAddress(final LatLonPoint latLonPoint) {
RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200,
GeocodeSearch.AMAP);// 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
geocoderSearch.getFromLocationAsyn(query);// 设置同步逆地理编码请求
}
/**
* 定位成功后回调函数
*/
@Override
public void onLocationChanged(AMapLocation aLocation) {
// TODO Auto-generated method stub
if (mListener != null && aLocation != null) {
mListener.onLocationChanged(aLocation);// 显示系统小蓝点
if (aMapLocation == null) {
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(aLocation.getLatitude(), aLocation.getLongitude()), 16));
aMapLocation = aLocation;
latLonPoint = new LatLonPoint(aMapLocation.getLatitude(), aMapLocation.getLongitude());
getAddress(latLonPoint);
}
}
}
/**
* 激活定位
*/
@Override
public void activate(OnLocationChangedListener listener) {
mListener = listener;
if (mAMapLocationManager == null) {
mAMapLocationManager = LocationManagerProxy.getInstance(this);
/*
* mAMapLocManager.setGpsEnable(false);
* 1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是true Location
* API定位采用GPS和网络混合定位方式
* ,第一个参数是定位provider,第二个参数时间最短是2000毫秒,第三个参数距离间隔单位是米,第四个参数是定位监听者
*/
mAMapLocationManager.requestLocationUpdates(
LocationProviderProxy.AMapNetwork, 2000, 10, this);
}
}
/**
* 停止定位
*/
@Override
public void deactivate() {
mListener = null;
if (mAMapLocationManager != null) {
mAMapLocationManager.removeUpdates(this);
mAMapLocationManager.destory();
}
mAMapLocationManager = null;
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics