Map API**做Google Android地图应用

通过上一节的讲解,已经申请到了一个Android Map API Key,下面开始讲解使用Map API**实现编程的基本流程。

  第1步:在文件AndroidManifest.xml中声明权限。

  在Anroid系统中,如果程序执行需要读取到安全敏感的项目,那么必须在AndroidManifest.xml中声明相关权限请求,比如这个地图程序需要从网络读取相关数据。所以必须声明android.permission.INTERNET权限。具体方法是在AndroidManifest.xml中添加如下代码。

<uses-permission android:name="android.permission.INTERNET" />
另外,因为maps类不是Android启动的默认类,所以还需要在文件AndroidManifest.xml的application 标签中申明要用maps类。

<uses-library android:name="com.google.android.maps" />
下面是基本的AndroidManifest.xml文件代码。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    <application android:icon="@drawable/icon" android:label="@string/app_name">
     <uses-library android:name="com.google.android.maps" />
    </application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
第2步:在main.xml主文件中完成Layout。

  下面开始着手来完成界面。假设设置要显示杭州的卫星地图,并在地图上方有5个按钮,分别可以放大地图、缩小地图或者切换显示模式(卫星,交通,街景)。即整个界面主要由2个部分组成,上面是一排5个按钮,下面是MapView。

  在Android中,LinearLayout是可以互相嵌套的,在此可以把上面5个按钮放在一个子LinearLayout里边(子LinearLayout的指定可以由android:addStatesFromChildren="true"实现),然后再把这个子LinearLayout加到外面的父LinearLayout里边。具体实现如下。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout android:layout_width="fill_parent"
  android:addStatesFromChildren="true"           /*说明是子Layout
  android:gravity="center_vertical"                /*这个子Layout里边的按钮是横向排列
  >

  <Button android:id="@+id/ZoomOut"
   android:text="放大"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginTop="5dip"            /*下面的4个属性,指定了按钮的相对位置
   android:layout_marginLeft="30dip"
   android:layout_marginRight="5dip"
   android:layout_marginBottom="5dip"
   android:padding="5dip" />

  /*其余4个按钮省略
</LinearLayout>
<com.google.android.maps.MapView
  android:id="@+id/map"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:enabled="true"
  android:clickable="true"
  android:apiKey="在此输入上一节申请的API Key"      /*必须加上上一节申请的API Key
/>

</LinearLayout>
第3步:完成主Java程序代码。

  首先,主文件的这个类必须继承MapActivity。

  public class Mapapp extends MapActivity {

  然后,来关注onCreate()函数,其核心代码如下。

public void onCreate(Bundle icicle) {
//取得地图View
  myMapView = (MapView) findViewById(R.id.map);
  //设置为卫星模式
  myMapView.setSatellite(true);
  //地图初始化的点:杭州
  GeoPoint p = new GeoPoint((int) (30.27 * 1000000),
    (int) (120.16 * 1000000));
  //取得地图View的控制
  MapController mc = myMapView.getController();
  //定位到杭州
  mc.animateTo(p);
  //设置初始化倍数
  mc.setZoom(DEFAULT_ZOOM_LEVEL);

接着,编写缩放按钮的处理代码,具体如下。

btnZoomIn.setOnClickListener(new View.OnClickListener() {
   public void onClick(View view) {
     myMapView.getController().setZoom(myMapView.getZoomLevel() - 1);
  });
地图模式的切换由下面代码实现。

btnSatellite.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
  myMapView.setSatellite(true);                    //卫星模式为True
  myMapView.setTraffic(false);                    //交通模式为False
  myMapView.setStreetView(false);                //街景模式为False
}
});
到此为止,就完成了第一个使用Map API的应用程序。

 

  在本节的内容中,将通过一个实例的实现过程来讲解使用Map API**实现google地图定位的基本流程。本实例源文件保存在“光盘:\daima\12\”中,命名为“CurrentLocationWithMap”。下面开始介绍本实例的具体实现流程。

  (1)编写主布局文件main.xml

  在布局文件main.xml中,插入了2个Button按钮,分别实现对地图的“放大”和“缩小”;然后,通过ToggleButton控制是否显示卫星地图;最后,设置申请的api Key。具体代码如下所示。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:id="@+id/myLocationText"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" >
    <Button 
        android:id="@+id/in" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="放大" />
    <Button 
        android:id="@+id/out" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="缩小" />
</LinearLayout>
<ToggleButton 
    android:id="@+id/switchMap"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textOff="卫星视图(关)" 
    android:textOn="卫星视图(开)"/>
<com.google.android.maps.MapView
    android:id="@+id/myMapView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey="0by7ffx8jX0A_LWXeKCMTWAh8CqHAlqvzetFqjQ"
     />
</LinearLayout>
(2)声明权限

  在文件AndroidManifest.xml中,需要声明android.permission.INTERNET和INTERNET权限,具体代码如下所示。

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
(3)编写主程序文件CurrentLocationWithMap.java

  第1步:通过方法onCreate将MapView绘制到屏幕上。因为MapView只能继承自MapActivity的活动中,所以必须用方法onCreate将MapView绘制到屏幕上,并同时覆盖方法isRouteDisplayed(),它表示是否需要在地图上绘制导航线路,具体代码如下所示。

package com.CurrentLocationWithMap;

import java.util.List;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

public class CurrentLocationWithMap extends MapActivity {
    
    MapView map;
    
    MapController ctrlMap;
    Button inBtn;
    Button outBtn;
    ToggleButton switchMap; 
    
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
第2步:定义方法onCreate,首先引入主布局main.xml,并通过方法findViewById获得MapView对象的引用,接着调用getOverlays()方法获取其Overylay链表,并将构建好的MyLocationOverlay对象添加到链表中去。其中MyLocationOverlay对象调用的enableMyLocation()方法表示尝试通过位置服务来获取当前的位置,具体代码如下所示。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
        
    map = (MapView)findViewById(R.id.myMapView); 
    List<Overlay> overlays = map.getOverlays();
    MyLocationOverlay myLocation = new MyLocationOverlay(this,map);
    myLocation.enableMyLocation();
    overlays.add(myLocation);
还需要为“放大”和“缩小”这2个按钮设置处理程序,首先通过方法getController()获取MapView的MapController对象,然后在“放大”和“缩小”两个按钮单击事件监听器的回放方法里,根据按钮的不同实现对MapView的缩放,具体代码如下所示。

ctrlMap = map.getController();
inBtn = (Button)findViewById(R.id.in);
outBtn = (Button)findViewById(R.id.out);
OnClickListener listener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.in:                            /*如果是缩放*/
            ctrlMap.zoomIn();
            break;
        case R.id.out:                        /*如果是放大*/
            ctrlMap.zoomOut();
            break;
        default:
            break;
        }
    }
};
inBtn.setOnClickListener(listener);
outBtn.setOnClickListener(listener);

//=======================================
第3步:通过方法onCheckedChanged来获取是否选择了switchMap,如果选择了则显示卫星地图。首先通过方法findViewById获取对应id的ToggleButton对象的引用,然后调用setOnCheckedChangeListener方法,设置对事件监听器选中的事件进行处理。根据ToggleButton是否被选中,进而通过setSatellite()方法启用或禁用卫星试图功能。具体代码如下所示。

       switchMap = (ToggleButton)findViewById(R.id.switchMap);
        switchMap.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton cBtn, boolean isChecked) {
                if (isChecked == true) {
                    map.setSatellite(true);
                } else {
                    map.setSatellite(false);
                }
            }
        });
第4步:首先通过LocationManager获取当前的位置,然后通过getBestProvider方法来获取和查询条件,最后设置更新位置信息的最小间隔为2s,位移变化在10m以上。具体代码如下所示。

    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);
    //String provider = LocationManager.GPS_PROVIDER;
        
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = locationManager.getBestProvider(criteria, true);
        
    Location location = locationManager.getLastKnownLocation(provider);
    updateWithNewLocation(location);
    locationManager.requestLocationUpdates(provider, 2000, 10,
            locationListener);
}
第5步:设置回调方法何时被调用,具体代码如下所示。

private final LocationListener locationListener = new LocationListener() {
  public void onLocationChanged(Location location) {
  updateWithNewLocation(location);
  }
  public void onProviderDisabled(String provider){
  updateWithNewLocation(null);
  }
  public void onProviderEnabled(String provider){ }
  public void onStatusChanged(String provider, int status,
  Bundle extras){ }
  };
第6步:定义方法updateWithNewLocation(Location location),用于显示地里信息和地图信息。具体代码如下所示。

private void updateWithNewLocation(Location location) {
        String latLongString;
        TextView myLocationText;
        myLocationText = (TextView)findViewById(R.id.myLocationText);
        if (location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            latLongString = "纬度:" + lat + "\n经度:" + lng;
            
            ctrlMap.animateTo(new GeoPoint((int)(lat*1E6),(int)(lng*1E6)));
        } else {
            latLongString = "无法获取地理信息";
        }
        myLocationText.setText("您当前的位置是:\n" +
        latLongString);
        
    }
}
至此,整个实例介绍完毕,在图12-9中选定一个经度和维度位置后,可以显示此位置的定位信息,并且定位信息分别以文字和地图形式显示出来,如图12-10所示。

应用实例:如何使用Map API**

▲图12-9 指定位置 图12-10 显示对应信息

  单击“放大”和“缩小”按钮后,能控制地图的大小显示,如图12-11所示。打开卫星试图后,可以显示此位置范围对应的卫星地图,如图12-12所示。


▲图12-11 放大后效果 图12-12 卫星地图
应用实例:如何使用Map API**










本文转自 wws5201985 51CTO博客,原文链接:http://blog.51cto.com/wws5201985/769736,如需转载请自行联系原作者