zoukankan      html  css  js  c++  java
  • 百度地图3.1.0(三)图层展示

    基于之前的文章 百度地图3.1.0(一)Hello BaiduMap(http://www.cnblogs.com/creasylai19/p/3921053.html) 添加的功能

    主要是添加了三个Button,代码如下

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical"
     6     tools:context="com.example.testbaidu_v_3_1.MainActivity" >
     7 
     8     <LinearLayout
     9         android:layout_width="match_parent"
    10         android:layout_height="wrap_content"
    11         android:orientation="horizontal" >
    12 
    13         <Button
    14             android:layout_width="0dp"
    15             android:layout_height="wrap_content"
    16             android:layout_weight="1"
    17             android:onClick="openNormalMode"
    18             android:text="普通地图" />
    19 
    20         <Button
    21             android:layout_width="0dp"
    22             android:layout_height="wrap_content"
    23             android:layout_weight="1"
    24             android:onClick="openSatelliteMode"
    25             android:text="卫星地图" />
    26 
    27         <Button
    28             android:layout_width="0dp"
    29             android:layout_height="wrap_content"
    30             android:layout_weight="1"
    31             android:onClick="openTraffic"
    32             android:text="交通图" />
    33     </LinearLayout>
    34 
    35     <com.baidu.mapapi.map.MapView
    36         android:id="@+id/bmapView"
    37         android:layout_width="fill_parent"
    38         android:layout_height="fill_parent"
    39         android:clickable="true" />
    40 
    41 </LinearLayout>

    MainActivity改成:

     1 package com.example.testbaidu_v_3_1;
     2 
     3 import android.content.BroadcastReceiver;
     4 import android.content.Context;
     5 import android.content.Intent;
     6 import android.content.IntentFilter;
     7 import android.os.Bundle;
     8 import android.support.v7.app.ActionBarActivity;
     9 import android.view.View;
    10 import android.widget.Toast;
    11 
    12 import com.baidu.mapapi.SDKInitializer;
    13 import com.baidu.mapapi.map.BaiduMap;
    14 import com.baidu.mapapi.map.MapView;
    15 
    16 public class MainActivity extends ActionBarActivity {
    17 
    18     private MapView mMapView = null;
    19     private SDKReceiver mReceiver;
    20     private BaiduMap mBaiduMap;
    21     private boolean trafficStatus = false;
    22 
    23     protected void onCreate(Bundle savedInstanceState) {
    24         super.onCreate(savedInstanceState);
    25         // 在使用SDK各组件之前初始化context信息,传入ApplicationContext
    26         // 注意该方法要再setContentView方法之前实现
    27         SDKInitializer.initialize(getApplicationContext());
    28         setContentView(R.layout.activity_main);
    29         // 获取地图控件引用
    30         mMapView = (MapView) findViewById(R.id.bmapView);
    31         mBaiduMap = mMapView.getMap();
    32 
    33         // 看baidu的Demo,发现它注册这个广播,用来监听SDKInitializer的初始化
    34         IntentFilter iFilter = new IntentFilter();
    35         iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);
    36         iFilter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);
    37         mReceiver = new SDKReceiver();
    38         registerReceiver(mReceiver, iFilter);
    39     }
    40 
    41     public void openNormalMode(View mView) {
    42         mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
    43     }
    44 
    45     public void openSatelliteMode(View mView) {
    46         mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
    47     }
    48 
    49     public void openTraffic(View mView) {
    50         if (trafficStatus) {
    51             mBaiduMap.setTrafficEnabled(false);
    52             trafficStatus = false;
    53         } else {
    54             mBaiduMap.setTrafficEnabled(true);
    55             trafficStatus = true;
    56         }
    57     }
    58 
    59     protected void onDestroy() {
    60         super.onDestroy();
    61         // 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
    62         mMapView.onDestroy();
    63         unregisterReceiver(mReceiver);
    64     }
    65 
    66     @Override
    67     protected void onResume() {
    68         super.onResume();
    69         // 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
    70         mMapView.onResume();
    71     }
    72 
    73     @Override
    74     protected void onPause() {
    75         super.onPause();
    76         // 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
    77         mMapView.onPause();
    78     }
    79 
    80     public class SDKReceiver extends BroadcastReceiver {
    81         public void onReceive(Context context, Intent intent) {
    82             String s = intent.getAction();
    83             if (s.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR)) {
    84                 Toast.makeText(MainActivity.this,
    85                         "key 验证出错! 请在 AndroidManifest.xml 文件中检查 key 设置",
    86                         Toast.LENGTH_SHORT).show();
    87                 ;
    88             } else if (s
    89                     .equals(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR)) {
    90                 Toast.makeText(MainActivity.this, "网络出错", Toast.LENGTH_SHORT)
    91                         .show();
    92             }
    93         }
    94     }
    95 
    96 }

    对比之前(一)的例子,可以看到主要是添加了  mBaiduMap = mMapView.getMap();

    并对参数mBaiduMap做控制

    运行结果截屏:

  • 相关阅读:
    CodeForces 734F Anton and School
    CodeForces 733F Drivers Dissatisfaction
    CodeForces 733C Epidemic in Monstropolis
    ZOJ 3498 Javabeans
    ZOJ 3497 Mistwald
    ZOJ 3495 Lego Bricks
    CodeForces 732F Tourist Reform
    CodeForces 732E Sockets
    CodeForces 731E Funny Game
    CodeForces 731D 80-th Level Archeology
  • 原文地址:https://www.cnblogs.com/creasylai19/p/3921185.html
Copyright © 2011-2022 走看看