你好,游客 登录 注册 搜索
背景:
阅读新闻

Android使用Google Map浅谈

[日期:2012-07-23] 来源:Linux社区  作者:Jimmy5z [字体:大 中 小]

创建一个 Android 工程

1.建立新的 Android 工程,并命名为 GoogleMapExample,开发基于 Google Map 的应用需要指定该工程的 build target 为 Google APIs。

图 4. 创建 Android 工程界

开发基于 Google Map 的 Android 应用

2.开发 Android 应用可以在手机或者模拟器上进行调试,如果使用模拟器进行开发,首先需要创建 Android 模拟器,并且指定该模拟器的 target 为 Google APIs。

图 5. 创建 Android 模拟器界面

开发基于 Google Map 的 Android 应用

3.设置示例工程的运行环境,确保示例工程运行在包含有 Google API 的目标模拟器上。

图 6. 设置示例工程运行环境界面

开发基于 Google Map 的 Android 应用

4.在模拟器上运行 GoogleMapExample 示例工程,模拟器应该有如图 7 的输出。自此,开发基于 Google Map 应用的工程就初步搭建起来了,接下来我们一步一步来将地图控件添加进 GoogleMapExample 应用中

图 7. 运行结果界面

开发基于 Google Map 的 Android 应用

将 Google Map 控件添加到 GoogleMapExample 工程中

首先,在 AndroidManifest.xml 文件中添加对于 Google Map 库的引用。通过使用 uses-library 标签来添加 google map 库到当前应用中。
清单 1. 添加对于 Google Map 库的引用

1
<uses-library android:name="com.google.android.maps" />

添加的相应的访问权限:

● android.permission.ACCESS_COARSE_LOCATION: 允许应用通过网络信息获取当前地理位置信息的权限。

● android.permission.ACCESS_FINE_LOCATION: 允许应用通过 GPS 获取当前地理位置信息的权限,较网络获得的地理位置信息更为精确。

● android.permission.INTERNET: 允许应用访问网络的权限。

最终的 AndroidManifest.xml 文件示例如下:
清单 2. AndroidManifest.xml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.mapExample"
      android:versionCode="1"
      android:versionName="1.0">
      <uses-sdk android:minSdkVersion="3" />
      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
      <uses-permission android:name="android.permission.INTERNET"/>
      <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
      <application android:icon="@drawable/icon" android:label="@string/app_name">
          <uses-library android:name="com.google.android.maps" />
<activity android:name=".GoogleMapExampleActivity"
              android:label="@string/app_name">
              <intent-filter>
                  <action android:name="android.intent.action.MAIN" />
                  <category android:name="android.intent.category.LAUNCHER" />
              </intent-filter>
          </activity>
      </application>
</manifest>

其次,将 MapView 控件添加进布局文件 main.xml 中,并将 1.3 中获得的 API 填入 MapView 控件的声明中。
清单 3. main.xml 文件、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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">
      <com.google.android.maps.MapView
           android:id="@+id/mapView"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:clickable="true"
           android:apiKey="0gtIEXVeu0g96nXpxjvByVW9hs7V0mDiuNHRwRw" />
</LinearLayout>

第三,将 GoogleMapExampleActivity 的继承类修改为 MapActivity,并添加 com.google.android.maps.MapActivity包,重写 isRouteDisplayed 方法,由于示例中没有正在显示的路线信息,这里我们返回 false 即可。
清单 4. GoogleMapExampleActivity 类

1
2
3
4
5
6
7
8
9
10
11
public class GoogleMapExampleActivity extends MapActivity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
      }
      @Override
      protected boolean isRouteDisplayed() {
           return false;
      }
}

编译并且运行,将会得到如图 8 所示的地图,这时地图控件已经被我们成功的添加进示例工程中了。

图 8. 世界地图界面

开发基于 Google Map 的 Android 应用

linux
相关资讯       Google Map 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 馒头 发表于 2013/3/3 20:59:36
求源码吖,初学 谢谢