在Swift基础 - - 高德地图实践(一)中使用swift来使用高德地图,这里使用Android实践高德地图包含定位,移动地图动画,获取网络数据,marker标记以及点击,Infowindow自定义界面以及点击,以及点击无marker地图上面隐藏infowindow等功能。直接看代码
package com.carmap.ui; import android.graphics.Color; import android.location.Location; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.AnimationUtils; import android.widget.ImageView; 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.maps.AMap; import com.amap.api.maps.AMap.InfoWindowAdapter; import com.amap.api.maps.AMap.OnCameraChangeListener; import com.amap.api.maps.AMap.OnInfoWindowClickListener; import com.amap.api.maps.AMap.OnMapClickListener; import com.amap.api.maps.AMap.OnMapLoadedListener; import com.amap.api.maps.AMap.OnMarkerClickListener; import com.amap.api.maps.CameraUpdateFactory; import com.amap.api.maps.LocationSource; import com.amap.api.maps.MapView; import com.amap.api.maps.UiSettings; import com.amap.api.maps.model.BitmapDescriptorFactory; import com.amap.api.maps.model.CameraPosition; import com.amap.api.maps.model.LatLng; import com.amap.api.maps.model.Marker; import com.amap.api.maps.model.MarkerOptions; import com.amap.api.maps.model.MyLocationStyle; import com.carmap.R; import com.carmap.ui.base.BaseActivity; /** * * @author jwzhangjie * */ public class MapActivity extends BaseActivity implements AMapLocationListener, LocationSource, OnMarkerClickListener, OnInfoWindowClickListener, InfoWindowAdapter, OnMapLoadedListener, OnCameraChangeListener, AnimationListener, OnClickListener, OnMapClickListener { private MapView mapView; private AMap aMap; private LocationManagerProxy aMapManager; private OnLocationChangedListener mListener; private UiSettings mUiSettings; private AMapLocation aLocation; private Animation centerMarker; private ImageView centerImageView; private Marker currentMarker; private boolean isFirst = true; private ImageView locate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map); mapView = (MapView) findViewById(R.id.mapView); mapView.onCreate(savedInstanceState);// 此方法必须重写 centerMarker = AnimationUtils.loadAnimation(this, R.anim.center_map_bounds); centerImageView = (ImageView) findViewById(R.id.centerMarkerImg); locate = (ImageView) findViewById(R.id.locate); initMap(); } /** * 初始化AMap对象 */ private void initMap() { if (aMap == null) { aMap = mapView.getMap(); mUiSettings = aMap.getUiSettings(); } // 自定义系统定位小蓝点 MyLocationStyle myLocationStyle = new MyLocationStyle(); myLocationStyle.myLocationIcon(BitmapDescriptorFactory .fromResource(R.drawable.transdrawable));// 设置小蓝点的图标 myLocationStyle.strokeColor(Color.argb(0, 0, 0, 0));// 设置圆形的边框颜色 myLocationStyle.radiusFillColor(Color.argb(0, 0, 0, 0));// 设置圆形的填充颜色 myLocationStyle.strokeWidth(0f);// 设置圆形的边框粗细 aMap.setMyLocationStyle(myLocationStyle); aMap.setMyLocationRotateAngle(180); aMap.setLocationSource(this);// 设置定位监听 mUiSettings.setMyLocationButtonEnabled(false); // 是否显示默认的定位按钮 aMap.setMyLocationEnabled(true);// 是否可触发定位并显示定位层 aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE); mUiSettings.setTiltGesturesEnabled(false);// 设置地图是否可以倾斜 mUiSettings.setScaleControlsEnabled(true);// 设置地图默认的比例尺是否显示 mUiSettings.setZoomControlsEnabled(false); initMapListener(); } private void initMapListener() { aMap.setOnMapLoadedListener(this); aMap.setOnCameraChangeListener(this); aMap.setOnMarkerClickListener(this); aMap.setOnInfoWindowClickListener(this); aMap.setInfoWindowAdapter(this);// 设置自定义InfoWindow样式 aMap.setOnMapClickListener(this); centerMarker.setAnimationListener(this); locate.setOnClickListener(this); } /** * 方法必须重写 */ @Override protected void onResume() { super.onResume(); mapView.onResume(); } /** * 方法必须重写 */ @Override protected void onPause() { super.onPause(); mapView.onPause(); } /** * 方法必须重写 */ @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } /** * 方法必须重写 */ @Override protected void onDestroy() { super.onDestroy(); mapView.onDestroy(); } @Override public void onLocationChanged(Location location) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } @Override public void onLocationChanged(AMapLocation aLocation) { if (aLocation != null) { this.aLocation = aLocation; if (mListener != null) mListener.onLocationChanged(aLocation);// 显示系统小蓝点 if (isFirst) { isFirst = false; aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng( aLocation.getLatitude(), aLocation.getLongitude()))); CameraUpdateFactory.zoomTo(16); MarkerOptions markerOption = new MarkerOptions(); markerOption.position(new LatLng(aLocation.getLatitude(), aLocation.getLongitude())); markerOption.title("上海市").snippet("上海:34.341568, 108.940174"); markerOption.draggable(true); Marker marker = aMap.addMarker(markerOption); marker.setObject("11");//这里可以存储用户数据 } } } @SuppressWarnings("deprecation") @Override public void activate(OnLocationChangedListener listener) { mListener = listener; if (aMapManager == null) { aMapManager = LocationManagerProxy.getInstance(this); /* * 1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是true */ // Location API定位采用GPS和网络混合定位方式,时间最短是2000毫秒 aMapManager.requestLocationUpdates( LocationProviderProxy.AMapNetwork, 2000, 10, this); } } /** * 停止定位 */ @Override public void deactivate() { mListener = null; if (aMapManager != null) { aMapManager.removeUpdates(this); aMapManager.destory(); } aMapManager = null; } @Override public boolean onMarkerClick(Marker marker) { currentMarker = marker; Log.e("marker", marker.getObject() + "marker: " + marker.getPosition().latitude+" : "+marker); return false; } @Override public View getInfoContents(Marker marker) { Log.e("marker", marker.getObject() + "getInfoContents: " + marker.getId()); return null; } @Override public View getInfoWindow(Marker marker) { Log.e("marker", marker.getObject() + "getInfoWindow: " + marker.getId()); View infoWindow = getLayoutInflater().inflate( R.layout.custom_info_window, null); render(marker, infoWindow); return infoWindow; } @Override public void onInfoWindowClick(Marker marker) { Log.e("marker", marker.getObject() + "onInfoWindowClick: " + marker.getId()); } @Override public void onMapLoaded() { centerImageView.startAnimation(centerMarker); CameraUpdateFactory.zoomTo(16); Log.e("load", "onMapLoaded"); } @Override public void onCameraChange(CameraPosition arg0) { } @Override public void onCameraChangeFinish(CameraPosition arg0) { centerImageView.startAnimation(centerMarker); Log.e("load", "onCameraChangeFinish+获取后台数据"); } @Override public void onAnimationStart(Animation animation) { centerImageView.setImageResource(R.drawable.green_pin_lift); } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { centerImageView.setImageResource(R.drawable.green_pin); } @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.locate: Log.e("load", "locate"); aMap.animateCamera(CameraUpdateFactory.changeLatLng(new LatLng( aLocation.getLatitude(), aLocation.getLongitude()))); break; default: break; } } /** * 自定义infowinfow窗口,动态修改内容的 */ public void render(Marker marker, View view) { } // 点击非marker区域,将显示的InfoWindow隐藏 @Override public void onMapClick(LatLng latLng) { if (currentMarker != null) { currentMarker.hideInfoWindow(); } } }
package com.carmap.ui.base; import android.content.Intent; import android.support.v4.app.FragmentActivity; /** * * @author jwzhangjie * */ public class BaseActivity extends FragmentActivity{ public Intent startIntent; public void startChange(Class name){ startIntent = new Intent(); startIntent.setClass(this, name); startActivity(startIntent); } public void startChange(Class name, boolean close){ startChange(name); if(close)finish(); } }
处于地图中心点的图片的弹跳动画设置如下,bounce_interpolator是一个弹跳的动画,在弹跳和停止是两张不同的图片,所以在onAnimationStart和onAnimationEnd分别设置。
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" android:fillEnabled="true" android:interpolator="@android:anim/bounce_interpolator" > <translate android:duration="1000" android:fromYDelta="-100%" android:toYDelta="0%" /> </set>
@Override public void onAnimationStart(Animation animation) { centerImageView.setImageResource(R.drawable.green_pin_lift); } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { centerImageView.setImageResource(R.drawable.green_pin); }
上面的MapActivity.java包含了以上功能
时间: 2024-10-05 04:09:34