• Android学习之高德地图的通用功能开发步骤(一)


    这次分享的心得是高德地图的开发,基本上都是按照高德官网上的API文档来进行开发,废话不多说,走起。

    第一步:申请高德地图的一个key,有了这个key我们的高德地图才可以正确用起来。

    至于怎么申请高德地图的key,其实很简单,这里我大概说一下步骤:打开网页http://lbs.amap.com,注册一个高德地图API的一个账号(这里我声明一下,并不是给高德打广告,我只是分享一下我的这次学习经历而已),注册成功之后,会提示你成为开发者,成为开发者之后就能获取一个key了,获取key需要填写的内容,可以在高德地图API官网上有,仔细阅读就好了,很简单的。

    第二步:下载我们开发使用的包,具体需要下载哪些,可以查看配置工程里面,都有写,在相关下载那里下载这些包

    把这些包都添加到我们自己新建的Android工程里面去,然后给我们的工程分配一些权限,以及填入自己的key

    第三步:前期准备工作已经基本上完成了,接下来就是使用高德地图API能实现自己的地图,第一个功能就是实现显示地图

    下面是我的ShowMapActivity的布局文件

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent" >
     5     
     6     <!-- 引入布局文件 -->
     7     <com.amap.api.maps.MapView
     8         xmlns:android="http://schemas.android.com/apk/res/android"
     9         android:id="@+id/map"
    10         android:layout_width="match_parent"
    11         android:layout_height="match_parent" />
    12     
    13 </RelativeLayout>
    View Code

    然后就是实现我的ShowMapActivity(基本上都是官方文档中的):

     1 package com.oysd.activity;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 
     6 import com.amap.api.maps.AMap;
     7 import com.amap.api.maps.MapView;
     8 import com.oysd.ouyangmap.R;
     9 import com.oysd.ouyangmap.R.id;
    10 import com.oysd.ouyangmap.R.layout;
    11 
    12 public class ShowMapActivity extends Activity {
    13     
    14     private MapView mapView;
    15     private AMap aMap;
    16     
    17     
    18     @Override
    19     protected void onCreate(Bundle savedInstanceState) {
    20         // TODO Auto-generated method stub
    21         super.onCreate(savedInstanceState);
    22         setContentView(R.layout.activity_showmap);
    23         mapView = (MapView) findViewById(R.id.map);
    24         mapView.onCreate(savedInstanceState);//必须要写
    25         init();
    26     }
    27     
    28     /**
    29      * 初始化AMap对象
    30      */
    31     private void init() {
    32         if (aMap == null) {
    33             aMap = mapView.getMap();
    34         }
    35     }
    36  
    37     /**
    38      * 方法必须重写
    39      */
    40     @Override
    41     protected void onResume() {
    42         super.onResume();
    43         mapView.onResume();
    44     }
    45  
    46     /**
    47      * 方法必须重写
    48      */
    49     @Override
    50     protected void onPause() {
    51         super.onPause();
    52         mapView.onPause();
    53     }
    54      
    55     /**
    56      * 方法必须重写
    57      */
    58     @Override
    59     protected void onSaveInstanceState(Bundle outState) {
    60         super.onSaveInstanceState(outState);
    61         mapView.onSaveInstanceState(outState);
    62     }
    63  
    64     /**
    65      * 方法必须重写
    66      */
    67     @Override
    68     protected void onDestroy() {
    69         super.onDestroy();
    70         mapView.onDestroy();
    71     }
    72 
    73 }
    View Code

    至于怎么样在手机上来显示这个activity我这里就不明说了,可以在首界面弄一个按钮,或者一个ListView,然后实现点击代码就ok了

    第一个功能是非常简单的,仅仅就是实现了显示地图

    第四步:实现定位功能

    要实现定位功能的话,还得去高德地图API官网去下载定位需要用到的包,需要哪些包,还是在配置工程那里查看,都有写的,耐心看就好了。

    包导进去之后,就可以写代码了,以下是我的LocationActivity的布局文件(和第一个功能的布局其实是一样的):

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent" >
     5     
     6     <!-- 引入布局文件 -->
     7     <com.amap.api.maps.MapView
     8         xmlns:android="http://schemas.android.com/apk/res/android"
     9         android:id="@+id/location"
    10         android:layout_width="match_parent"
    11         android:layout_height="match_parent" />
    12     
    13 
    14 </RelativeLayout>
    View Code

    然后就是LocationActivity的实现代码(我也是参考官方文档写的):

      1 package com.oysd.activity;
      2 
      3 import android.app.Activity;
      4 import android.location.Location;
      5 import android.os.Bundle;
      6 import android.util.Log;
      7 
      8 import com.amap.api.location.AMapLocation;
      9 import com.amap.api.location.AMapLocationListener;
     10 import com.amap.api.location.LocationManagerProxy;
     11 import com.amap.api.location.LocationProviderProxy;
     12 import com.amap.api.maps.AMap;
     13 import com.amap.api.maps.LocationSource;
     14 import com.amap.api.maps.MapView;
     15 import com.amap.api.maps.LocationSource.OnLocationChangedListener;
     16 import com.oysd.ouyangmap.R;
     17 import com.oysd.ouyangmap.R.id;
     18 import com.oysd.ouyangmap.R.layout;
     19 
     20 public class LocationActivity extends Activity implements LocationSource, AMapLocationListener {
     21     
     22     private MapView mapView;
     23     private AMap aMap;
     24     private LocationManagerProxy mLocationManagerProxy;
     25     private OnLocationChangedListener mListener;
     26     
     27     private static final String TAG = "LocationActivity";
     28     
     29     @Override
     30     protected void onCreate(Bundle savedInstanceState) {
     31         // TODO Auto-generated method stub
     32         super.onCreate(savedInstanceState);
     33         setContentView(R.layout.activity_location);
     34         mapView = (MapView) findViewById(R.id.location);
     35         mapView.onCreate(savedInstanceState);
     36         init();
     37     }
     38     
     39     /**
     40      * 初始化AMap对象
     41      */
     42     private void init(){
     43         if(aMap == null ){
     44             aMap = mapView.getMap();
     45         }
     46         //initLocation();
     47         setUpMap();
     48     }
     49     
     50     
     51     /**
     52      * 初始化定位
     53      */
     54     private void initLocation(){
     55         
     56         mLocationManagerProxy = LocationManagerProxy.getInstance(this);
     57         //此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
     58         //注意设置合适的定位时间的间隔,并且在合适时间调用removeUpdates()方法来取消定位请求
     59         //在定位结束后,在合适的生命周期调用destroy()方法     
     60         //其中如果间隔时间为-1,则定位只定一次
     61         mLocationManagerProxy.requestLocationData(
     62                 LocationProviderProxy.AMapNetwork, -1, 15, this);
     63  
     64         mLocationManagerProxy.setGpsEnable(false);
     65     }
     66     
     67     private void setUpMap(){
     68         aMap.setLocationSource(this);// 设置定位监听
     69         aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
     70         aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
     71         // 设置定位的类型为定位模式:定位(AMap.LOCATION_TYPE_LOCATE)、跟随(AMap.LOCATION_TYPE_MAP_FOLLOW)
     72         // 地图根据面向方向旋转(AMap.LOCATION_TYPE_MAP_ROTATE)三种模式
     73         aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
     74         
     75     }
     76 
     77     /**
     78      * 激活定位
     79      */
     80     @Override
     81     public void activate(OnLocationChangedListener onLocationChangedListener) {
     82         // TODO Auto-generated method stub
     83         mListener = onLocationChangedListener;
     84         if (mLocationManagerProxy == null) {
     85                 mLocationManagerProxy = LocationManagerProxy.getInstance(this);
     86                 //此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
     87                 //注意设置合适的定位时间的间隔,并且在合适时间调用removeUpdates()方法来取消定位请求
     88                 //在定位结束后,在合适的生命周期调用destroy()方法
     89                 //其中如果间隔时间为-1,则定位只定一次
     90                 mLocationManagerProxy.requestLocationData(
     91                         LocationProviderProxy.AMapNetwork, -1, 10, this);
     92         }
     93     }
     94 
     95     
     96     /**
     97      * 停止定位
     98      */
     99     @Override
    100     public void deactivate() {
    101         // TODO Auto-generated method stub
    102         mListener = null;
    103         if (mLocationManagerProxy != null) {
    104             mLocationManagerProxy.removeUpdates(this);
    105             mLocationManagerProxy.destroy();
    106         }
    107         mLocationManagerProxy = null;
    108     }
    109 
    110     @Override
    111     public void onLocationChanged(Location location) {
    112         // TODO Auto-generated method stub
    113         
    114     }
    115 
    116     @Override
    117     public void onProviderDisabled(String provider) {
    118         // TODO Auto-generated method stub
    119         
    120     }
    121 
    122     @Override
    123     public void onProviderEnabled(String provider) {
    124         // TODO Auto-generated method stub
    125         
    126     }
    127 
    128     @Override
    129     public void onStatusChanged(String provider, int status, Bundle extras) {
    130         // TODO Auto-generated method stub
    131         
    132     }
    133 
    134     @Override
    135     public void onLocationChanged(AMapLocation aMapLocation) {
    136         // TODO Auto-generated method stub
    137         if(aMapLocation != null && aMapLocation.getAMapException().getErrorCode() == 0){
    138             //获取位置信息
    139             Double geoLat = aMapLocation.getLatitude();
    140             Double geoLng = aMapLocation.getLongitude();  
    141             Log.d(TAG, "Latitude = " + geoLat.doubleValue() + ", Longitude = " + geoLng.doubleValue());
    142             
    143             // 通过 AMapLocation.getExtras() 方法获取位置的描述信息,包括省、市、区以及街道信息,并以空格分隔。
    144             String desc = "";
    145             Bundle locBundle = aMapLocation.getExtras();
    146             if (locBundle != null) {
    147                     desc = locBundle.getString("desc");
    148                     Log.d(TAG, "desc = " + desc);
    149             }
    150             mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点
    151         }
    152     }
    153     
    154     /**
    155      * 此方法需存在
    156      */
    157     @Override
    158     protected void onResume() {
    159         super.onResume();
    160         mapView.onResume();
    161     }
    162  
    163     /**
    164      * 此方法需存在
    165      */
    166     @Override
    167     protected void onPause() {
    168         super.onPause();
    169         mapView.onPause();
    170         deactivate();
    171     }
    172  
    173     /**
    174      * 此方法需存在
    175      */
    176     @Override
    177     protected void onDestroy() {
    178         super.onDestroy();
    179         mapView.onDestroy();
    180     }
    181  
    182 }
    View Code

    这样的话,就实现了地图定位功能了

    第五步:获取自己位置的天气信息

    以下是我的WeatherActivity的布局文件(超简陋的,勿喷):

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent" >
     5     
     6 
     7     <TextView 
     8         android:layout_width="fill_parent"
     9         android:layout_height="fill_parent"
    10         android:id="@+id/tv_weather"
    11         android:textColor="@android:color/black"
    12         android:textSize="28sp"/>
    13     
    14 </RelativeLayout>
    View Code

    然后是实现WeatherActivity的代码:

     1 package com.oysd.activity;
     2 
     3 import java.util.List;
     4 
     5 import android.app.Activity;
     6 import android.os.Bundle;
     7 import android.widget.TextView;
     8 import android.widget.Toast;
     9 
    10 import com.amap.api.location.AMapLocalDayWeatherForecast;
    11 import com.amap.api.location.AMapLocalWeatherForecast;
    12 import com.amap.api.location.AMapLocalWeatherListener;
    13 import com.amap.api.location.AMapLocalWeatherLive;
    14 import com.amap.api.location.LocationManagerProxy;
    15 import com.oysd.ouyangmap.R;
    16 import com.oysd.ouyangmap.R.id;
    17 import com.oysd.ouyangmap.R.layout;
    18 
    19 public class WeatherActivity extends Activity implements AMapLocalWeatherListener {
    20     
    21     private LocationManagerProxy mLocationManagerProxy;
    22     private TextView tvWeather;
    23     
    24     @Override
    25     protected void onCreate(Bundle savedInstanceState) {
    26         // TODO Auto-generated method stub
    27         super.onCreate(savedInstanceState);
    28         setContentView(R.layout.activity_weather);
    29         tvWeather = (TextView) findViewById(R.id.tv_weather);
    30         init();
    31     }
    32     
    33     private void init(){
    34         mLocationManagerProxy = LocationManagerProxy.getInstance(this);
    35         mLocationManagerProxy.requestWeatherUpdates(
    36                     LocationManagerProxy.WEATHER_TYPE_FORECAST, this);
    37     }
    38 
    39     @Override
    40     public void onWeatherForecaseSearched(AMapLocalWeatherForecast aMapLocalWeatherForecast) {
    41         // TODO Auto-generated method stub
    42         if(aMapLocalWeatherForecast != null && aMapLocalWeatherForecast.getAMapException().getErrorCode() == 0){
    43              
    44             List<AMapLocalDayWeatherForecast> forcasts = aMapLocalWeatherForecast
    45                     .getWeatherForecast();
    46             for (int i = 0; i < forcasts.size(); i++) {
    47                 AMapLocalDayWeatherForecast forcast = forcasts.get(i);
    48                 switch (i) {
    49                 //今天天气
    50                 case 0:
    51                                         //城市
    52                     String city = forcast.getCity();
    53                                         String today = "今天 ( "+ forcast.getDate() + " )";
    54                     String todayWeather = forcast.getDayWeather() + "    "
    55                             + forcast.getDayTemp() + "/" + forcast.getNightTemp()
    56                             + "    " + forcast.getDayWindPower();
    57                     tvWeather.setText("城市:" + city + ", " + today + ", 天气信息:" + todayWeather);
    58                     break;
    59                 //明天天气
    60                 case 1:
    61                      
    62                     String tomorrow = "明天 ( "+ forcast.getDate() + " )";
    63                     String tomorrowWeather = forcast.getDayWeather() + "    "
    64                             + forcast.getDayTemp() + "/" + forcast.getNightTemp()
    65                             + "    " + forcast.getDayWindPower();
    66                     tvWeather.append("; " + tomorrow + ", 天气信息:" + tomorrowWeather);
    67                     break;
    68                 //后天天气
    69                 case 2:
    70                      
    71                     String aftertomorrow = "后天( "+ forcast.getDate() + " )";
    72                     String aftertomorrowWeather = forcast.getDayWeather() + "    "
    73                             + forcast.getDayTemp() + "/" + forcast.getNightTemp()
    74                             + "    " + forcast.getDayWindPower();
    75                     tvWeather.append("; " + aftertomorrow + ", 天气信息:" + aftertomorrowWeather);
    76                     break;
    77                 }
    78             }
    79         }else{
    80             // 获取天气预报失败
    81             Toast.makeText(this,"获取天气预报失败:"+ aMapLocalWeatherForecast.getAMapException().getErrorMessage(), Toast.LENGTH_SHORT).show();
    82         }
    83         
    84     }
    85 
    86     @Override
    87     public void onWeatherLiveSearched(AMapLocalWeatherLive arg0) {
    88         // TODO Auto-generated method stub
    89         
    90     }
    91 
    92 }
    View Code

    第六步:实现不同地图类型的显示、实时路况以及截图(矢量地图模式、卫星地图模式、夜景地图模式):

    下面是我的MapTypeActivity的布局文件:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent" >
     5     
     6     <!-- 引入布局文件 -->
     7     <com.amap.api.maps.MapView
     8         android:id="@+id/map"
     9         android:layout_width="match_parent"
    10         android:layout_height="match_parent" 
    11         />
    12     
    13     <LinearLayout
    14         android:layout_width="wrap_content"
    15         android:layout_height="wrap_content"
    16         android:layout_alignParentTop="true"
    17         android:layout_alignParentLeft="true"
    18         android:layout_marginLeft="11dp"
    19         android:layout_marginTop="10dp"
    20         android:orientation="vertical">
    21  
    22         <RadioGroup
    23             android:id="@+id/map_type_rg"
    24             android:layout_width="wrap_content"
    25             android:layout_height="wrap_content"
    26             android:orientation="horizontal">
    27  
    28             <RadioButton
    29                 android:id="@+id/map_type_normal_rb"
    30                 android:layout_width="wrap_content"
    31                 android:layout_height="wrap_content"
    32                 android:checked="true"
    33                 android:text="Normal"/>
    34  
    35             <RadioButton
    36                 android:id="@+id/map_type_satellite_rb"
    37                 android:layout_width="wrap_content"
    38                 android:layout_height="wrap_content"
    39                 android:text="Satellite"/>
    40  
    41             <RadioButton
    42                 android:id="@+id/map_type_night_rb"
    43                 android:layout_width="wrap_content"
    44                 android:layout_height="wrap_content"
    45                 android:text="Night"/>
    46  
    47         </RadioGroup>
    48         
    49         <CheckBox
    50             android:id="@+id/trafficCB"
    51             android:layout_width="wrap_content"
    52             android:layout_height="wrap_content"
    53             android:text="显示实时路况"/>
    54         
    55         <Button
    56             android:id="@+id/screenShotBtn"
    57             android:layout_width="wrap_content"
    58             android:layout_height="wrap_content"
    59             android:text="截屏"
    60             android:onClick="screenShot"/>
    61  
    62     </LinearLayout>
    63     
    64 
    65 </RelativeLayout>
    View Code

    然后就是我的MapTypeActivity的实现代码:

      1 package com.oysd.activity;
      2 
      3 import java.io.FileNotFoundException;
      4 import java.io.FileOutputStream;
      5 import java.io.IOException;
      6 import java.text.SimpleDateFormat;
      7 import java.util.Date;
      8 
      9 import android.app.Activity;
     10 import android.graphics.Bitmap;
     11 import android.graphics.Color;
     12 import android.location.Location;
     13 import android.os.Bundle;
     14 import android.os.Environment;
     15 import android.view.View;
     16 import android.widget.Button;
     17 import android.widget.CheckBox;
     18 import android.widget.CompoundButton;
     19 import android.widget.RadioGroup;
     20 import android.widget.Toast;
     21 import android.widget.RadioGroup.OnCheckedChangeListener;
     22 
     23 import com.amap.api.location.AMapLocation;
     24 import com.amap.api.location.AMapLocationListener;
     25 import com.amap.api.location.LocationManagerProxy;
     26 import com.amap.api.location.LocationProviderProxy;
     27 import com.amap.api.maps.AMap;
     28 import com.amap.api.maps.LocationSource;
     29 import com.amap.api.maps.MapView;
     30 import com.amap.api.maps.AMap.OnMapScreenShotListener;
     31 import com.amap.api.maps.LocationSource.OnLocationChangedListener;
     32 import com.amap.api.maps.model.BitmapDescriptorFactory;
     33 import com.amap.api.maps.model.MyLocationStyle;
     34 import com.amap.api.navi.AMapNavi;
     35 import com.oysd.ouyangmap.R;
     36 import com.oysd.ouyangmap.R.drawable;
     37 import com.oysd.ouyangmap.R.id;
     38 import com.oysd.ouyangmap.R.layout;
     39 
     40 public class MapTypeActivity extends Activity implements LocationSource, AMapLocationListener, OnMapScreenShotListener {
     41     
     42     private static final String TAG = "MapTypeActivity";
     43     
     44     private MapView mMapView;
     45     private AMap aMap;
     46     private RadioGroup mAMapTypesRG;
     47     private CheckBox mTrafficCB;
     48     
     49     private OnLocationChangedListener mListener;
     50     private LocationManagerProxy mLocationManagerProxy;
     51     private Button screenShotBtn;
     52     private AMapNavi mapNavi;
     53     
     54     @Override
     55     protected void onCreate(Bundle savedInstanceState) {
     56         // TODO Auto-generated method stub
     57         super.onCreate(savedInstanceState);
     58         setContentView(R.layout.activity_map_type);
     59         
     60         mMapView = (MapView) findViewById(R.id.map);
     61         mMapView.onCreate(savedInstanceState);
     62         initAMap();
     63     }
     64     
     65     /**
     66      * 初始化AMap对象
     67      */
     68     private void initAMap(){
     69         if(aMap == null){
     70             aMap = mMapView.getMap();
     71         }
     72         
     73         findViewById();
     74         setUpMap();
     75         setUpNaviMap();
     76     }
     77     
     78     
     79     private void setUpMap(){
     80         // 设置定位监听。如果不设置此定位资源则定位按钮不可点击。
     81         aMap.setLocationSource(this);
     82         // 设置默认定位按钮是否显示
     83         aMap.getUiSettings().setMyLocationButtonEnabled(true);
     84         // 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
     85         aMap.setMyLocationEnabled(true);
     86         //设置定位的类型为定位模式 ,可以由定位、跟随或地图根据面向方向旋转几种
     87         aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_FOLLOW);
     88         // 自定义系统定位蓝点
     89         MyLocationStyle myLocationStyle = new MyLocationStyle();
     90         // 自定义定位蓝点图标
     91         myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.location_marker));
     92         // 自定义精度范围的圆形边框颜色
     93         myLocationStyle.strokeColor(Color.BLACK);
     94         // 设置圆形的填充颜色
     95         myLocationStyle.radiusFillColor(Color.argb(100, 0, 0, 180));
     96         //自定义精度范围的圆形边框宽度
     97         myLocationStyle.strokeWidth(5);
     98         // 将自定义的 myLocationStyle 对象添加到地图上
     99         aMap.setMyLocationStyle(myLocationStyle);
    100         // 构造 LocationManagerProxy 对象
    101         mLocationManagerProxy = LocationManagerProxy.getInstance(MapTypeActivity.this);
    102         
    103         
    104     }
    105     
    106     private void setUpNaviMap(){
    107         mapNavi = AMapNavi.getInstance(this);
    108         
    109     }
    110     /**
    111      * 获取单选圆框控件
    112      */
    113     private void findViewById(){
    114         mAMapTypesRG = (RadioGroup) findViewById(R.id.map_type_rg);
    115         mTrafficCB = (CheckBox) findViewById(R.id.trafficCB);
    116         screenShotBtn = (Button) findViewById(R.id.screenShotBtn);
    117         
    118         setListeners();
    119     }
    120     
    121     /**
    122      * 设置监听器
    123      */
    124     private void setListeners(){
    125         mAMapTypesRG.setOnCheckedChangeListener(this.myOnCheckedChangeListener);
    126         mTrafficCB.setOnCheckedChangeListener(this.myCBOnCheckedChangeListener);
    127         screenShotBtn.setOnClickListener(this.myOnClickListener);
    128     }
    129     
    130     View.OnClickListener myOnClickListener = new View.OnClickListener() {
    131         
    132         @Override
    133         public void onClick(View v) {
    134             // TODO Auto-generated method stub
    135             screenShot(v);
    136         }
    137     };
    138     
    139     private void screenShot(View v){
    140         // 设置截屏监听接口,截取地图可视区域
    141         aMap.getMapScreenShot(this);
    142     }
    143     RadioGroup.OnCheckedChangeListener myOnCheckedChangeListener = new OnCheckedChangeListener() {
    144         
    145         @Override
    146         public void onCheckedChanged(RadioGroup group, int checkedId) {
    147             // TODO Auto-generated method stub
    148             switch(checkedId){
    149             //矢量地图模式
    150             case R.id.map_type_normal_rb:
    151                 aMap.setMapType(AMap.MAP_TYPE_NORMAL);
    152                 break;
    153             //卫星地图模式
    154             case R.id.map_type_satellite_rb:
    155                 aMap.setMapType(AMap.MAP_TYPE_SATELLITE);
    156                 break;
    157             //夜景地图模式
    158                 
    159             case R.id.map_type_night_rb:
    160                 aMap.setMapType(AMap.MAP_TYPE_NIGHT);
    161                 break;
    162             }
    163         }
    164     };
    165     
    166     CompoundButton.OnCheckedChangeListener myCBOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
    167         
    168         @Override
    169         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    170             // TODO Auto-generated method stub
    171             aMap.setTrafficEnabled(isChecked);
    172         }
    173     };
    174 
    175 
    176     @Override
    177     public void activate(OnLocationChangedListener onLocationChangedListener) {
    178         // TODO Auto-generated method stub
    179         mListener = onLocationChangedListener;
    180         if (mLocationManagerProxy == null) {
    181             mLocationManagerProxy = LocationManagerProxy.getInstance(this);
    182             /*
    183              * mAMapLocManager.setGpsEnable(false);
    184              * 1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是true Location
    185              * API定位采用GPS和网络混合定位方式
    186              * ,第一个参数是定位provider,第二个参数时间最短是2000毫秒,第三个参数距离间隔单位是米,第四个参数是定位监听者
    187              */
    188             //此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
    189             //注意设置合适的定位时间的间隔,并且在合适时间调用removeUpdates()方法来取消定位请求
    190             //在定位结束后,在合适的生命周期调用destroy()方法
    191             //其中如果间隔时间为-1,则定位只定一次
    192             mLocationManagerProxy.requestLocationData(LocationProviderProxy.AMapNetwork, -1, 10, this);
    193         }
    194     }
    195 
    196     
    197     /**
    198      * 停止定位
    199      */
    200     @Override
    201     public void deactivate() {
    202         // TODO Auto-generated method stub
    203         mListener = null;
    204         if (mLocationManagerProxy != null) {
    205             mLocationManagerProxy.removeUpdates(this);
    206             mLocationManagerProxy.destroy();
    207         }
    208         mLocationManagerProxy = null;
    209     }
    210 
    211     @Override
    212     public void onLocationChanged(Location location) {
    213         // TODO Auto-generated method stub
    214         
    215     }
    216 
    217     @Override
    218     public void onProviderDisabled(String provider) {
    219         // TODO Auto-generated method stub
    220         
    221     }
    222 
    223     @Override
    224     public void onProviderEnabled(String provider) {
    225         // TODO Auto-generated method stub
    226         
    227     }
    228 
    229     @Override
    230     public void onStatusChanged(String provider, int status, Bundle extras) {
    231         // TODO Auto-generated method stub
    232         
    233     }
    234 
    235     @Override
    236     public void onLocationChanged(AMapLocation aMapLocation) {
    237         // TODO Auto-generated method stub
    238         if (mListener != null && aMapLocation != null) {
    239             mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点
    240         }
    241     }
    242 
    243     @Override
    244     public void onMapScreenShot(Bitmap bitmap) {
    245         // TODO Auto-generated method stub
    246         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    247         try {
    248             // 保存在SD卡根目录下,图片为png格式。
    249             FileOutputStream fos = new FileOutputStream(
    250                     Environment.getExternalStorageDirectory() + "/test_"
    251                             + sdf.format(new Date()) + ".png");
    252             boolean b = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    253             try {
    254                 fos.flush();
    255             } catch (IOException e) {
    256                 e.printStackTrace();
    257             }
    258             try {
    259                 fos.close();
    260             } catch (IOException e) {
    261                 e.printStackTrace();
    262             }
    263             if (b)
    264                 Toast.makeText(this, "截屏成功", Toast.LENGTH_SHORT).show();
    265             else {
    266                 Toast.makeText(this, "截屏失败", Toast.LENGTH_SHORT).show();
    267             }
    268         } catch (FileNotFoundException e) {
    269             e.printStackTrace();
    270         }
    271     }
    272 }
    View Code

    不过这里截图成功的图片需要通过一定的步骤才能看到:

    “开始”  输入“cmd” 进入如下截图路径下(具体情况具体分析,反正是进入adb目录下,要连着真机测试哦)

    输入:adb shell 

    然后输入:cd sdcard

    查看命令:ls

    看到那张图片的名字,然后导出到我们的电脑上进行查看:

    接着上一步的查看命令来,再输入:exit

    退出来输入:adb pull sdcard/文件名 D:/

    然后在D盘的根目录下查看此截图

    未完待续。。。。

  • 相关阅读:
    cookie小结
    WEB服务器,TOMCAT和servlet之间的关系
    HTTP协议基础总结
    servlet总结
    注解总结
    常使用的String方法
    变量&&常量
    标识符&&注释&&关键字
    配置环境变量&&OpenJDK和OracleJDK区别
    逆向工程__Generate插件安装 && xml配置文件解析 &&使用反向生成代码 && 接口说明
  • 原文地址:https://www.cnblogs.com/ouyangduoduo/p/4619407.html
Copyright © 2020-2023  润新知