Android开发GoogleMap入门

移动开发 Android
开发基于谷歌地图的应用和普通的android应用差不多都要使用它提供给我们的类库,所不同的是google map的类库不是android平台的基本类库,是google api的一部分,所以建立项目时,SDK要选择Google APIs;还有一点,开发基于地图的应用时候需要使用google map的APIkey,必须先申请key,然后才能开发基于地图的应用。

Android开发要连接GoogelMaps使用MapView时需要,先从Google网站申请一组经过验证的Maps API Key授权码,这个在网上很多。

[代码] [Java]代码

package com.android.antking.map; 
    import com.google.android.maps.MapActivity; 
    import com.google.android.maps.GeoPoint; 
    import com.google.android.maps.MapController; 
    import com.google.android.maps.MapView; 
    import android.os.Bundle; 
    public class MyMain extends MapActivity { 
        //放大倍数 
       static final int  INITIAL_ZOOM_LEVEL=12
       //中心点 
       static final int  INITIAL_LATITUDE=123465465
       static final int  INITIAL_LONGITUDR=15634646
        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.main); 
            MapView mapView = (MapView) findViewById(R.id.mapView); 
            //enable to change the size of the map 
            mapView.setBuiltInZoomControls(true); 
            //设置Zoom 大小和地图的中心点 
            MapController mc = mapView.getController(); 
            mc.setZoom(INITIAL_ZOOM_LEVEL); 
            mc.setCenter(new GeoPoint(INITIAL_LATITUDE,INITIAL_LONGITUDR)); 
        } 
        @Override 
        protected boolean isRouteDisplayed() { 
            // TODO Auto-generated method stub 
            return false
        } 
    } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

[代码] [XML]代码

<?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: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="0FZLYf-YM4SRrJrJum55MeeaO4Gd_IitVFmtUeA"/>这个是自己开发的google mapApi 密钥 
    </LinearLayout> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

[代码] 在这里加上google的地图包,还有Internet权限

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          package="com.android.antking.map" 
          android:versionCode="1" 
          android:versionName="1.0"
        <uses-sdk android:minSdkVersion="7" /> 
        <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
        <application android:icon="@drawable/icon" android:label="@string/app_name"
            <activity android:name=".MyMain" 
                      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> 
    </manifest> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

1.创建工程,注意SDK旋转为"Goolge APIs”

2.修改AndroidManifest.xml文件

由于使用Google Map API,所以必须添加<uses-library android:name="com.google.android.maps" />

由于需要从网络获取地图数据,所以需要访问网络的权限<uses-permission android:name="android.permission.INTERNET"/>

可能还需要添加其他权限。

3.创建MapView

4.实现MapActivity

5.MapController的使用

6.Ovelay的使用

责任编辑:闫佳明 来源: oschina
相关推荐

2011-09-09 14:09:17

Android Wid

2013-01-16 14:41:14

Android开发Android SDK

2013-01-16 15:07:30

Android NDKAndroid开发

2013-05-20 15:42:22

2009-03-11 13:18:57

Android入门Android开发Android模拟器

2010-02-06 15:31:18

ibmdwAndroid

2009-07-22 14:55:16

ibmdwAndroid

2010-02-04 17:59:50

Android SDK

2013-01-10 13:50:25

Android开发组件

2013-01-16 17:34:32

Android开发路线图

2012-04-26 22:57:57

Android

2013-01-17 16:17:33

Android开发Intent组件

2017-04-10 15:47:50

Android Stujni开发入门

2017-11-27 13:09:00

AndroidGradle代码

2013-01-17 15:51:42

Android开发应用程序组件

2014-05-08 13:36:07

Android Wea开发者预览版

2013-01-10 14:21:24

Android开发组件Activities

2010-08-26 08:57:04

AndroidAndroid开发

2012-01-10 09:56:32

WPF

2009-10-21 12:45:07

linux程序开发基础入门
点赞
收藏

51CTO技术栈公众号