您的位置:华清远见教育科技集团 >> Android资料 >> Android开发之使用Google地图  
 
Android开发之使用Google地图
分享到:

使用Google地图需要使用到Android定义的com.google.android.maps包,其中包含了一系列用于Google Map上显示、控制和层叠信息的功能类。该类主要包括以下几个类。

□ MapActivity:这个类用语显示Google Map的Activity类,它需要连接底层网络。任何想要显示MapView的Activity都需要继承MapActivity,并在其onCreate()方法中创建一个MapView对象。
    □ MapView的地图的显示控件,可以设置不同的显示模式,例如卫星模式、街道模式或交通模式。
    □ MapController则是MapView的控制器,可以控制MapView的显示中心和缩放级别等功能。
    □ Overlay:可显示于地图之上的可绘制对象。
    □ GeoPoint:包含经纬度位置的对象。

以下内容以GoogleMapDemo为例,说明如何在Android系统中开发Google地图程序。图1是GoogleMapDemo示例的效果图。

 
图1 地图模式、卫星模式效果图

在这个示例中,将在程序内部设置一个坐标点,然后在程序启动时,使用MapView控件在地图上显示这个坐标点的位置。

在建立工程时将com.google.android.maps的扩展库添加到工程中,这样就可以使用Google地图的所有功能。在创建工程时,在Build Target项中选择Google APIs;创建工程后,修改/res/layout/main.xml文件,在布局中加入一个MapView控件,并设置刚获取的“地图密钥”。main.xml文件的完整代码如代码清单1所示。

代码清单1 main.xml


<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello"/>

        <com.google.android.maps.MapView
            android:id="@+id/mapview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:enabled="true"
            android:clickable="true"
            android:apiKey="0mVK8GeO6WUz4S94z52CIGSSlvlTwnrE4DsiA"/>
        </LinearLayout>

仅在布局中添加MapView控件,还不能够直接在程序中调用这个控件,还需要将程序本身设置成MapActivity(com.google.android.maps.MapActivity)。MapActivity类负责处理显示Google地图所需的生命周期和后台服务管理。

下面先给出整个GoogleMapDemo.java文件的完整代码,如代码清单2所示。

代码清单2 GoogleMapDemo.java


package cn.com.farsight.GoogleMapDemo;

    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 android.os.Bundle;

    public class GoogleMapDemo extends MapActivity {
        private MapView mapView;
        private MapController mapController;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            mapView = (MapView)findViewById(R.id.mapview);
            //获取了MapController
            mapController = mapView.getController();

            //设定经度、纬度的地理坐标点
            Double lng = 116.391483 * 1E6;
            Double lat = 39.9055472 * 1E6;
            GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());

            //将这个坐标转化为GeoPoint再使用;设置MapView的“显示中点”
            mapController.setCenter(point);
            //设置放大层级
            mapController.setZoom(11);
            //将MapView显示区域的中心移动到“显示中心”
            mapController.animateTo(point);

            //设定MapView的地图显示模式。true则为卫星模式,设置false则为普通模式
            mapView.setSatellite(false);
        }

        //是统计程序是否显示在Google地图中显示路径信息,默认为不显示
        @Override
        protected boolean isRouteDisplayed() {
            return false;
        }
    }

由于获取Google地图是需要使用互联网的,所以在运行前还需要在AndroidManifest.xml文件中添加允许访问互联网的许可。AndroidManifest.xml文件的完整代码如代码清单3所示。

代码清单3 AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="//schemas.android.com/apk/res/android"
        package="cn.com.farsight.GoogleMapDemo"
        android:versionCode="1"
        android:versionName="1.0">
    <application android:icon="@drawable/icon"
            android:label="@string/app_name">
        <activity android:name=".GoogleMapDemo"
                  android:label="@string/app_name">
              <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
              </intent-filter>
        </activity>
        <uses-library android:name="com.google.android.maps"></uses-library>
    </application>

        <uses-sdk android:minSdkVersion="7" />
        <uses-permission android:name="android.permission.INTERNET"/>
    </manifest>

 更多相关文章

·Android开发之Google地图应用
·Android开发之GPS定位
·Android开发之Android Location API
·Android开发之Camera照相功能实现
·Android开发之音/视频的录制