Android高德地图API使用(1)

高德api地址:http://lbs.amap.com/
此文学习如何使用高德api进行2d图片显示,定位和添加锚点。
分享个库文件,demo,文档打包下载的链接:http://pan.baidu.com/s/1dDo2Z9R

配置

用到的包:
包
清单文件添加代码:

<application
         android:icon="@drawable/icon"
         android:label="@string/app_name" >

         <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="请输入您的用户Key"/>

         <activity android:name="com.amap.demo.LocationManager" >
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
</application>

添加权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

代码分析

主要做到以下功能:

  • 显示地图
  • 定位
  • 添加锚点
  • 自定义锚点图标,与圆形区域
  • 锚点点击动画

布局代码

显示地图布局代码(activity_main.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <com.amap.api.maps2d.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

自定义锚点内容布局(custom_info_contents.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/custom_info_contents_badge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:adjustViewBounds="true" >
    </ImageView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/custom_info_contents_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:ellipsize="end"
            android:singleLine="true"
            android:textColor="#ff000000"
            android:textSize="14dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/custom_info_contents_snippet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:singleLine="true"
            android:textColor="#ff7f7f7f"
            android:textSize="14dp" />
    </LinearLayout>

</LinearLayout>

自定义锚点提示框布局(custom_info_window.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="@drawable/custom_info_bubble"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/custom_info_contents_badge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:adjustViewBounds="true" >
    </ImageView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/custom_info_contents_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:ellipsize="end"
            android:singleLine="true"
            android:textColor="#ff000000"
            android:textSize="14dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/custom_info_contents_snippet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:singleLine="true"
            android:textColor="#ff7f7f7f"
            android:textSize="14dp" />
    </LinearLayout>

</LinearLayout>

主要逻辑代码如下

使用 MapView 类,必须重载 Activity 生命周期的所有方法,有 onCreate(),onDestroy(),onResume(),onPause(),onSaveInstanceState()。

代码注释很详细,请看代码:

package com.yunmo.mymapdemo.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

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.InfoWindowAdapter;
import com.amap.api.maps2d.AMap.OnInfoWindowClickListener;
import com.amap.api.maps2d.AMap.OnMapLoadedListener;
import com.amap.api.maps2d.AMap.OnMarkerClickListener;
import com.amap.api.maps2d.AMap.OnMarkerDragListener;
import com.amap.api.maps2d.AMapOptions;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptor;
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.yunmo.mymapdemo.R;

//onCreate(),onDestroy(),onResume(),onPause(),onSaveInstanceState()
public class MainActivity extends Activity implements LocationSource,
        AMapLocationListener, OnMarkerDragListener, OnMapLoadedListener,
        OnMarkerClickListener, OnInfoWindowClickListener, InfoWindowAdapter {

    private MapView mapView;
    private AMap aMap;
    private LocationManagerProxy mLocationManagerProxy;
    private OnLocationChangedListener mListener;
    private MarkerOptions markerOption;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);
        initLocation();
        initMap();
        initMarker();
    }

    /**
     * 初始化定位
     * 
     * @注册监听 requestLocationData(String provider, long minTime, float
     *       minDistance, AMapLocationListener listener)
     * @param provider
     *            :有三种定位Provider供用户选择 ,分别是:
     * 
     * @see LocationManagerProxy.GPS_PROVIDER,代表使用手机GPS定位 ;
     * @see LocationManagerProxy.NETWORK_PROVIDER ,代表使用手机网络定位;
     * @see LocationProviderProxy.AMapNetwork,代表高德网络定位服务, 混合定位。
     * 
     * @param minTime
     *            :位置变化的通知时间,单位为毫秒 。如果为-1,定位只定位一次。
     * @param minDistance
     *            :位置变化通知距离,单位为米。
     * @param listener
     *            :定位监听者。
     */
    private void initLocation() {
        mLocationManagerProxy = LocationManagerProxy.getInstance(this);

        // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
        // 注意设置合适的定位时间的间隔,并且在合适时间调用removeUpdates()方法来取消定位请求
        // 在定位结束后,在合适的生命周期调用destroy()方法
        // 其中如果间隔时间为-1,则定位只定一次
        mLocationManagerProxy.requestLocationData(
                LocationProviderProxy.AMapNetwork, 60 * 1000, 15, this);

        mLocationManagerProxy.setGpsEnable(false);
    }

    /**
     * 停止定位,并销毁定位资源
     */
    private void stopLocation() {
        if (mLocationManagerProxy != null) {
            mLocationManagerProxy.removeUpdates(this);
            mLocationManagerProxy.destory();
        }
        mLocationManagerProxy = null;
    }

    /**
     * 初始化地图
     */
    private void initMap() {
        if (aMap == null) {
            aMap = mapView.getMap();
            // aMap.setMapType(AMap.MAP_TYPE_SATELLITE);//卫星图示
            aMap.setMapType(AMap.MAP_TYPE_NORMAL);// 实时图示
            // aMap.setTrafficEnabled(true);// 实时交通
            // aMap.getUiSettings().setZoomControlsEnabled(true);//内置的缩放控制键,显示在地图的右下角。默认情况下是开启的
            aMap.getUiSettings().setLogoPosition(
                    AMapOptions.LOGO_POSITION_BOTTOM_LEFT);// 设置logo的位置
            aMap.getUiSettings().setCompassEnabled(true);// 设置指南针
            aMap.getUiSettings().setZoomGesturesEnabled(true);
            aMap.getUiSettings().setScrollGesturesEnabled(true);
            float scale = aMap.getScalePerPixel();// 一像素代表多少米
            aMap.getUiSettings().setScaleControlsEnabled(true);// 设置显示地图的默认比例尺

            setUpMap();// 更改定位图标
        }
    }

    /**
     * 初始化Marker
     */
    private void initMarker() {
        aMap.setOnMarkerDragListener(this);// 设置marker可拖拽事件监听器
        aMap.setOnMapLoadedListener(this);// 设置amap加载成功事件监听器
        aMap.setOnMarkerClickListener(this);// 设置点击marker事件监听器
        aMap.setOnInfoWindowClickListener(this);// 设置点击infoWindow事件监听器
        aMap.setInfoWindowAdapter(this);// 设置自定义InfoWindow样式
        addMarkerToMap();
    }

    /**
     * demo用,实际使用时重写该方法
     */
    private void addMarkerToMap() {
        markerOption = new MarkerOptions();
        markerOption.position(new LatLng(34.341568, 108.940174));// 设置经纬度
        markerOption.anchor(0.5f, 0.5f);// 定义marker 图标的锚点。锚点是marker
                                        // 图标接触地图平面的点。图标的左顶点为(0,0)点,右底点为(1,1)点。
        markerOption.title("标题").snippet("摘要");
        markerOption.icons(anchorGifIcon());// 锚点显示图标
        // markerOption.draggable(true);//拖拽,不建议
         markerOption.period(10);//
        // period:获取或设置Marker对象的刷新周期,单位:毫秒。该属性用于设置Marker动画效果各帧的切换频率,配合
        // AMapMarkerOptions.IconUris
        // 属性能够实现自定义的动画效果。

        aMap.addMarker(markerOption).setRotateAngle(90);
        // aMap.addMarker(markerOption).setRotateAngle(90) //旋转90度

        Marker marker = aMap.addMarker(new MarkerOptions()
                .position(new LatLng(34.7466, 113.625367))
                .title("好好学习")
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                .draggable(true));
        marker.showInfoWindow();// 设置默认显示一个infowinfow
    }

    /**
     * 锚点动画效果,蓝红黄三色显示
     */
    private ArrayList<BitmapDescriptor> anchorGifIcon() {
        ArrayList<BitmapDescriptor> giflist = new ArrayList<BitmapDescriptor>();
        giflist.add(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
        giflist.add(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_RED));
        giflist.add(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
        return giflist;
    }

    /**
     * 更改定位图标
     */
    private void setUpMap() {
        // 自定义系统定位蓝点
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        // 自定义定位蓝点图标
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory
                .fromResource(R.drawable.ic_launcher));
        // 自定义精度范围的圆形边框颜色
        myLocationStyle.strokeColor(Color.BLACK);
        // 自定义精度范围的圆形边框宽度
        myLocationStyle.strokeWidth(5);

        // 设置圆形的填充颜色
        myLocationStyle.radiusFillColor(Color.argb(100, 0, 0, 180));
        // 设置圆形的边框粗细
        myLocationStyle.strokeWidth(1.0f);
        // 将自定义的 myLocationStyle 对象添加到地图上
        aMap.setMyLocationStyle(myLocationStyle);

        // 构造 LocationManagerProxy 对象
        mLocationManagerProxy = LocationManagerProxy
                .getInstance(MainActivity.this);
        // 设置定位资源。如果不设置此定位资源则定位按钮不可点击。
        aMap.setLocationSource(this);
        // 设置默认定位按钮是否显示
        aMap.getUiSettings().setMyLocationButtonEnabled(true);
        // 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
        aMap.setMyLocationEnabled(true);

    }

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

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

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

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void deactivate() {
        if (mLocationManagerProxy != null) {
            mLocationManagerProxy.removeUpdates(this);
            mLocationManagerProxy.destory();
        }
        mLocationManagerProxy = null;
    }

    @Override
    public void onLocationChanged(Location arg0) {
    }

    @Override
    public void onProviderDisabled(String arg0) {
    }

    @Override
    public void onProviderEnabled(String arg0) {
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    }

    /**
     * 激活定位
     */
    @Override
    public void activate(OnLocationChangedListener arg0) {
        mListener = arg0;
        if (mLocationManagerProxy == null) {
            mLocationManagerProxy = LocationManagerProxy.getInstance(this);
            /*
             * mAMapLocManager.setGpsEnable(false);
             * 1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是true Location
             * API定位采用GPS和网络混合定位方式
             * ,第一个参数是定位provider,第二个参数时间最短是2000毫秒,第三个参数距离间隔单位是米,第四个参数是定位监听者
             */
            mLocationManagerProxy.requestLocationUpdates(
                    LocationProviderProxy.AMapNetwork, 2000, 10, this);
        }
    }

    /**
     * 定位回调
     * 
     * @see 使用GPS定位时,可获取定位速度(getSpeed()),定位方向(getBearing())。
     * @see 使用网络定位时, 返回省名称(如果是直辖市,省名称为null)(getProvince()),城市名称(getCity()),
     *      城市编码(getCityCode ()),区(县)名称(getDistrict()),区域编码(getAdCode()),街道和门牌信息
     *      (getStreet()),详细地址(getAddress()),描述信息(getExtras())。
     * 
     */
    @Override
    public void onLocationChanged(AMapLocation arg0) {
        if (arg0 != null && arg0.getAMapException().getErrorCode() == 0) {
            if (mListener != null) {
                mListener.onLocationChanged(arg0);// 显示系统小蓝点
            }
//          aMap.moveCamera(CameraUpdateFactory.zoomTo(15));// 重新设置地图视窗,无动画
            aMap.animateCamera(CameraUpdateFactory.zoomTo(12));//动画移动缩放
            // 获取位置信息
            Double geoLat = arg0.getLatitude();// 纬度
            Double geoLng = arg0.getLongitude();// 经度

            // 获取位置的描述信息,包括省、市、区以及街道信息,并以空格分隔
            String desc = "";
            Bundle locBundle = arg0.getExtras();
            if (locBundle != null) {
                desc = locBundle.getString("desc");
            }

            // GPS定位
            if (arg0.getProvider().equals("gps")) {

            }
            // 网络定位
            if (arg0.getProvider().equals("lbs")) {

            }
        }
    }

    /**
     * 自定义infocontents弹出框内容布局
     */
    @Override
    public View getInfoContents(Marker arg0) {

        // View view =
        // getLayoutInflater().inflate(R.layout.custom_info_contents,
        // null);// 加载自定义布局文件
        // customInfoContentsView(arg0, view);
        // return view;

        return null;// 这里可以自行判断并定义

    }

    /**
     * 自定义弹出框控件初始化
     * 
     * @param marker
     * @param view
     */
    private void customInfoContentsView(Marker marker, View view) {
        ((ImageView) view.findViewById(R.id.custom_info_contents_badge))
                .setImageResource(R.drawable.ic_launcher);

        String titleString = marker.getTitle();// 获得title文本
        TextView titleTextView = (TextView) view
                .findViewById(R.id.custom_info_contents_title);
        if (titleString != null) {
            SpannableString spannableString = new SpannableString(titleString);// 设置高亮
            spannableString.setSpan(new ForegroundColorSpan(Color.RED), 0,
                    titleString.length(), 0);// setSpan (Object what, int start
                                                // 开始位置, int end 结束位置, int
                                                // flags)
            titleTextView.setText(spannableString);
        } else {
            titleTextView.setText("");
        }

        String snippet = marker.getSnippet();// 获得摘要
        TextView snippetTextView = (TextView) view
                .findViewById(R.id.custom_info_contents_snippet);
        if (snippet != null) {
            SpannableString snippetText = new SpannableString(snippet);
            snippetText.setSpan(new ForegroundColorSpan(Color.GREEN), 0,
                    snippetText.length(), 0);
            snippetTextView.setText(snippetText);
        } else {
            snippetTextView.setText("");
        }

    }

    /**
     * 自定义infowindow窗口布局
     */
    @Override
    public View getInfoWindow(Marker arg0) {
        View view = getLayoutInflater().inflate(R.layout.custom_info_window,
                null);
        customInfoContentsView(arg0, view);
        return view;

    }

    /**
     * 锚点窗口信息被点击
     */
    @Override
    public void onInfoWindowClick(Marker arg0) {// 锚点窗口信息被点击
        // 操作;;;;;;;
        Toast.makeText(MainActivity.this, "锚点窗口被点击", Toast.LENGTH_SHORT).show();
    }

    /**
     * 锚点被点击
     */
    @Override
    public boolean onMarkerClick(Marker arg0) {// 锚点被点击

        // 写了个点击锚点后图标变了下的功能,如果原先的锚点是动态的不要设置Period否则原先的动态图会失效。
        final Marker clickMarker = arg0;
        final ArrayList<BitmapDescriptor> icons = clickMarker.getIcons();
        final long start = SystemClock.uptimeMillis();// 从开机到现在的毫秒数(手机睡眠的时间不包括在内);
        clickMarker.setIcon(BitmapDescriptorFactory
                .fromResource(R.drawable.ic_launcher));
        final Handler handler = new Handler();
        final long duration = 1500;// 延迟1.5秒
        handler.post(new Runnable() {

            @Override
            public void run() {
                long elapsed = SystemClock.uptimeMillis() - start;// 从开始到现在的时间间隔;
                float t = elapsed / duration;

                if (t < 1) {
                    handler.postDelayed(this, duration);
                } else {
                    clickMarker.setIcons(anchorGifIcon());
                    clickMarker.setIcons(icons);

                    aMap.invalidate();// 刷新map
                }
            }
        });

        Toast.makeText(MainActivity.this, "锚点被点击", Toast.LENGTH_SHORT).show();
        return false;

    }

    @Override
    public void onMapLoaded() {// 地图加载完毕
    }

    @Override
    public void onMarkerDrag(Marker arg0) {// 拖拽中
    }

    @Override
    public void onMarkerDragEnd(Marker arg0) {// 拖拽结束
    }

    @Override
    public void onMarkerDragStart(Marker arg0) {// 拖拽开始
    }
}

maodiankuang

maodian

图示:

图示

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值