Using Google Maps in Android

Android集成Google Maps实现真实道路导航路线绘制 在移动开发中,‘两点间路线绘制’常被误解为简单几何绘图,实则涉及地理坐标系统、道路拓扑建模与动态路径渲染等核心概念。其原理基于调用专业路线规划服务(如Google Directions API)获取带转向指令与真实道路走向的经纬度序列,再经坐标解码、屏幕适配、图层叠加等步骤完成高保真渲染。该技术具备精准定位、交互响应、缩放自适应等工程价值,广泛应用于物流调度、本地生活、出行服务等场景。本文聚焦 Android 平台下 Google Maps SDK 与 Directions API 的协同实践,深入解析 P 阅读详情

Using Google Maps in Android

来源:http://mobiforge.com/developing/story/using-google-maps-android?page=1

 

Section Feature image

Google Maps is one of the many applications bundled with the Android platform. In addition to simply using the Maps application, you can also embed it into your own applications and make it do some very cool things. In this article, I will show you how to use Google Maps in your Android applications and how to programmatically perform the following:

  1. Change the views of Google Maps
  2. Obtain the latitude and longitude of locations in Google Maps
  3. Perform geocoding and reverse geocoding
  4. Add markers to Google Maps

Creating the Project


Using Eclipse, create a new Android project and name GoogleMaps as shown in Figure 1.


Figure 1 Creating a new Android project using Eclipse

 

Obtaining a Maps API key


Beginning with the Android SDK release v1.0, you need to apply for a free Google Maps API key before you can integrate Google Maps into your Android application. To apply for a key, you need to follow the series of steps outlined below. You can also refer to Google's detailed documentation on the process athttp://code.google.com/android/toolbox/apis/mapkey.html.

First, if you are testing the application on the Android emulator, locate the SDK debug certificate located in the default folder of "C:/Documents and Settings/<username>/Local Settings/Application Data/Android". The filename of the debug keystore is debug.keystore. For deploying to a real Android device, substitute the debug.keystore file with your own keystore file. In a future article I will discuss how you can generate your own keystore file.

For simplicity, copy this file (debug.keystore) to a folder in C:/ (for example, create a folder called "C:/Android").

Using the debug keystore, you need to extract its MD5 fingerprint using the Keytool.exe application included with your JDK installation. This fingerprint is needed to apply for the free Google Maps key. You can usually find the Keytool.exefrom the "C:/Program Files/Java/<JDK_version_number>/bin" folder.

Issue the following command (see also Figure 2) to extract the MD5 fingerprint.

keytool.exe -list -alias androiddebugkey -keystore "C:/android/debug.keystore" -storepass android -keypass android

Copy the MD5 certificate fingerprint and navigate your web browser to: http://code.google.com/android/maps-api-signup.html. Follow the instructions on the page to complete the application and obtain the Google Maps key.


Figure 2 Obtaining the MD5 fingerprint of the debug keystore

To use the Google Maps in your Android application, you need to modify your AndroidManifest.xml file by adding the<uses-library> element together with the INTERNET permission:

 

Displaying the Map


To display the Google Maps in your Android application, modify the main.xml file located in the res/layout folder. You shall use the <com.google.android.maps.MapView> element to display the Google Maps in your activity. In addition, let's use the <RelativeLayout> element to position the map within the activity:

 

Notice from above that I have used the Google Maps key that I obtained earlier and put it into the apiKey attribute.

In the MapsActivity.java file, modify the class to extend from the MapActivity class, instead of the normal Activity class:

 

Observe that if your class extends the MapActivity class, you need to override the isRouteDisplayed() method. You can simply do so by setting the method to return false.

That's it! That's all you need to do to display the Google Maps in your application. Press F11 in Eclipse to deploy the application onto an Android emulator. Figure 3 shows the Google map in all its glory.


Figure 3 Google Maps in your application

At this juncture, take note of a few troubleshooting details. If your program does not run (i.e. it crashes), then it is likely you forgot to put the following statement in your AndroidManifest.xml file:

    

If your application manages to load but you cannot see the map (all you see is a grid), then it is very likely you do not have a valid Map key, or that you did not specify the INTERNET permission:

    

Displaying the Zoom View


The previous section showed how you can display the Google Maps in your Android device. You can drag the map to any desired location and it will be updated on the fly. However, observe that there is no way to zoom in or out from a particular location. Thus, in this section, you will learn how you can let users zoom into or out of the map.

First, add a <LinearLayout> element to the main.xml file as shown below:

 

You will use the <LinearLayout> element to hold the two zoom controls in Google Maps (you will see this shortly).

In the MapsActivity.java file, add the following imports:

import com.google.android.maps.MapView.LayoutParams;  
import android.view.View;
import android.widget.LinearLayout;

and add the following code after the line setContentView(R.layout.main);

  

The complete MapsActivity.java file is given below:

 

Basically, you obtain the MapView instance on the activity, obtain its zoom controls and then add it to the LinearLayoutelement you added to the activity earlier on. In the above case, the zoom control will be displayed at the bottom of the screen. When you now press F11 in Eclipse, you will see the zoom controls when you touch the map (see Figure 4).


Figure 4 Using the zoom controls in Google Maps

Using the zoom control, you can zoom in or out of a location by simply touching the "+ or "-" buttons on the screen.

Alternatively, you can also programmatically zoom in or out of the map using the zoomIn() and zoomOut() methods from the MapController class:

 

In the above code, when the user presses the number 3 on the keyboard the map will zoom in into the next level. Pressing number 1 will zoom out one level.

 

Changing Views of the Map


By default, the Google Maps displays in the map mode. If you wish to display the map in satellite view, you can use thesetSatellite() method of the MapView class, like this:

  

You can also display the map in street view, using the setStreetView() method:

   

Figure 5 shows the Google Maps displayed in satellite and street views, respectively.


Figure 5 Displaying Google Maps in satellite and street views

 

Displaying a Particular Location


Be default, the Google Maps displays the map of the United States when it is first loaded. However, you can also set the Google Maps to display a particular location. In this case, you can use the animateTo() method of the MapControllerclass.

The following code shows how this is done:

 

In the above code, you first obtain a controller from the MapView instance and assign it to a MapController object (mc). You use a GeoPoint object to represent a geographical location. Note that for this class the latitude and longitude of a location are represented in micro degrees. This means that they are stored as integer values. For a latitude value of 40.747778, you need to multiply it by 1e6 to obtain 40747778.

To navigate the map to a particular location, you can use the animateTo() method of the MapController class (an instance which is obtained from the MapView object). The setZoom() method allows you to specify the zoom level in which the map is displayed. Figure 6 shows the Google Maps displaying the map of Singapore.


Figure 6 Navigating to a particular location on the map

 

Adding Markers


Very often, you may wish to add markers to the map to indicate places of interests. Let's see how you can do this in Android. First, create a GIF image containing a pushpin (see Figure 7) and copy it into the res/drawable folder of the project. For best effect, you should make the background of the image transparent so that it does not block off parts of the map when the image is added to the map.


Figure 7 Adding an image to the res/drawable folder

To add a marker to the map, you first need to define a class that extends the Overlay class:

 

In the MapOverlay class that you have defined, override the draw() method so that you can draw the pushpin image on the map. In particular, note that you need to translate the geographical location (represented by a GeoPoint object, p) into screen coordinates.

As you want the pointed tip of the push pin to indicate the position of the location, you would need to deduct the height of the image (which is 50 pixels) from the y-coordinate of the point (see Figure 8) and draw the image at that location.


Figure 8 Adding an image to the map

To add the marker, create an instance of the MapOverlap class and add it to the list of overlays available on the MapViewobject:

   

Figure 9 shows how the pushpin looks like when added to the map.


Figure 9 Adding a marker to the map

 

Getting the Location that was touched


After using Google Maps for a while, you may wish to know the latitude and longitude of a location corresponding to the position on the screen that you have just touched. Knowing this information is very useful as you can find out the address of a location, a process known as Geocoding (you will see how this is done in the next section).

If you have added an overlay to the map, you can override the onTouchEvent() method within the Overlay class. This method is fired every time the user touches the map. This method has two parameters - MotionEvent and MapView. Using the MotionEvent parameter, you can know if the user has lifted his finger from the screen using the getAction() method. In the following code, if the user has touched and then lifted his finger, you will display the latitude and longitude of the location touched:

 

  Figure 10 shows this in action.


Figure 10 Displaying the latitude and longitude of a point touched on the map

 

Geocoding and Reverse Geocoding


If you know the latitude and longitude of a location, you can find out its address using a process known as Geocoding. Google Maps in Android supports this via the Geocoder class. The following code shows how you can find out the address of a location you have just touched using the getFromLocation() method:

 

    Figure 11 shows the above code in action.


Figure 11 Performing Geocoding in Google Maps

If you know the address of a location but want to know its latitude and longitude, you can do so via reverse-Geocoding. Again, you can use the Geocoder class for this purpose. The following code shows how you can find the exact location of the Empire State Building by using the getFromLocationName() method:

   

Once the location is found, the above code navigates the map to the location. Figure 12 shows the code in action.


Figure 12 Navigating to the Empire State Building

 

Summary


In this article, you have learnt a few tricks for the Google Maps in Android. Using Google Maps, there are many interesting projects you can work on, such as geo-tagging, geo-tracking, etc. If you have cool ideas on building cool location-based services, share with us in the comments box below. Have fun!

 

Android应用中的Google Maps API基础集成 Google Maps API是Google提供的强大地图服务API集合,允许开发者在自己的应用程序中嵌入Google地图。通过这一系列API,可以实现地图的展示、地理数据的查询、路径规划等功能。Google Maps API覆盖了从网页应用到移动应用的广泛平台,是构建位置相关的服务不可或缺的工具。MapView控件除了基本的尺寸和位置属性,还提供了一些特定的属性来控制地图的展示。例如,mapType属性可以用来指定地图类型(如正常地图、卫星地图、混合地图等)。 阅读详情

相关推荐

基于知识图谱的电影推荐系统——Neo4j&Python

也可直接从这里下载:链接: https://pan.baidu.com/s/1l6wjwcUzy5G_dIlVDbCkpw 提取码: pkq6。修改main.py中的driver,输入自己数据库的用户名与密码。系统会询问是否需要重新加载并创建知识图谱,在第一次时输入1。

weixin_46351593的博客 6361

Example of Using Google Maps Service and GPS in Android

 Introduction This tip is about using Google Maps v2 services and GPS in Android. Background Please refer to the below mentioned article to setup your eproject in Android Studio. https://deve

tomcat的专栏 925

DINOv2 基于自定义数据集进行图片分类预测

(前提是安装好pytorch)1. pip安装2. 下载dinov2提供的预训练模型,需要自行前往网站上查找dinov2模型进行下载。模型需要下载三个文件:下载完成后将上述三个文件放置在本地一个名为dinov2_base的文件夹中即可。

sinat_34770838的博客 6671

Google Maps Android Places Demos 教程

Google Maps Android Places Demos 教程 项目介绍 Google Maps Android Places Demos 是一个开源项目,旨在展示如何在 Android 应用中集成 Google Places API。该项目提供了多个示例,帮助开发者快速理解和使用 Google Places API 的各种功能,包括地点搜索、地点详情、地点自动完成等。 项目快速启动 环境...

gitblog_00993的博客 877

surfer10与Arcgis10.3 完美生成等值线和等值面方法

surfer10 与Arcgis10.3生成等值线和等值面的步骤如下: 1.准备surfer用的Book1.dat文件,格式如下: "x" "y" "z" 116.471992 40.809975 232 116.335556 40.700542 1235 116.541626 40.713333 214 116.73349 40.798607 566 2.在surfer中,选择Gr

哈里·雪利博客 1万+

深入解析:如何在Visual Studio中高效编译libredwg与libdxfrw

本文详细介绍了在Visual Studio中高效编译libredwg与libdxfrw的完整流程,包括环境配置、源码获取、CMake技巧及常见错误解决方案。针对CAD开发者,提供了从基础编译到项目集成的实用指南,帮助快速处理DWG/DXF文件,提升开发效率。

weixin_29215391的博客 234

如何在iPhone和Android上更改Google Maps Voice

The Google Maps app offers hands-free directions, travel alerts, and more for users. Its built-in voice engine offers this in your own preferred voice, with options based on region or language. If you...

culintai3473的博客 2036

deepfake detection数据集

最全Deepfake数据集总结,包含各个数据集下载链接: https://github.com/deepfakeinthewild/deepfake_in_the_wild Dataset name Download Generate method Deepfake videos Actors Deepfake-TIMIT low download De...

where_is_horse的博客 1万+

Android Google Maps教程

Android平台在Android应用程序和Google Maps之间提供了轻松而紧密的集成。 引擎盖下使用了完善的Google Maps API ,以便将Google Maps的功能引入您的Android应用程序。 在本教程中,我们将了解如何将Google Maps集成到Android应用中。 安装Google API 为了能够使用Google Maps,必须在您的SDK中提供Goog...

cunfen6312的博客 699

Redis Lua引擎UAF漏洞CVE-2025-49844深度剖析与复现实践

内存安全是软件系统稳定性的基石,其核心原理在于确保程序对内存的访问始终处于合法、受控的状态。在C/C++等手动管理内存的语言中,Use-After-Free(释放后使用)是一种典型的高危内存安全漏洞,它源于已释放的内存区域仍被程序指针引用,导致数据混乱或程序崩溃。这类漏洞的技术价值在于其往往能突破高级语言的安全边界,直接威胁进程本身,危害等级极高,常见于数据库、浏览器等核心基础设施。在Redis这样的高性能内存数据库中,其内置的Lua脚本引擎为复杂操作提供了强大支持,但同时也引入了沙箱环境下的复杂内存对象生

weixin_42530732的博客 372

网络层隧道技术之ICMP隧道(pingTunnel/IcmpTunnel)

目录 ICMP隧道 使用ICMP搭建隧道(PingTunnel) 使用ICMP搭建隧道(Icmptunnel) ICMP隧道 ICMP隧道简单实用,是一个比较特殊的协议。在一般的通信协议里,如果两台设备要进行通信,肯定需要开放端口,而在ICMP协议下就不需要。最常见的ping命令就是利用的ICMP协议,攻击者可以利用命令行得到比回复更多的ICMP请求。在通常情况下,每个ping命令都有相...

谢公子的博客 6712

Android Google Maps

在国内你选择的SDK可以是高德、百度、腾讯等,但在国外,你首选肯定是谷歌,因此要进行Google地图的开发你首先要解决下面三个问题VPNGoogle账号信用卡American Express(美国运通卡)Discover(美国发现卡)JCB(Japan Credit Bureau,日本国际信用卡)MasterCard(万事达)VISA(维萨)

晨曦 5366

Linux 下 OpenClaw 安装全记录:从零到一的保姆级图文教程

本文主要介绍了Linux 下 OpenClaw 安装全记录:从零到一的保姆级图文教程,希望能对学习使用OpenClaw 的同学们有所帮助。 文章目录 1. 背景 2. 实战教程 2.1 步骤一 安装Node.js 2.2 步骤二:安装git 2.3 步骤三:安装并配置OpenClaw 2.4 步骤四:配置OpenClaw 2.5 步骤五:配置模型 2.6 步骤六:对话 3. 后续使用步骤 4. 报错解决方案 5. OpenClaw的卸载

weixin_43178406的博客 11万+

【亲测免费】 使用 Google Maps Android KTX 开发指南

使用 Google Maps Android KTX 开发指南 项目介绍 Google Maps Android KTX 是一个为 Google Maps SDK for Android 和 Maps SDK for Android Utility Library 提供的 Kotlin 扩展库。这个开源项目旨在通过提供 Kotlin 语言特性,如扩展函数、命名参数和默认参数,使开发者能够编写更简洁...

gitblog_01098的博客 725

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

免费下载谷歌maps软件_Android Google Maps示例教程

谷歌maps菜单语言设置In this tutorial we’ll discuss and implement some interesting features of android google maps API in our application. Before we get onto the discussion. Please make sure that you’ve been t...

从零开始的教程世界 1274

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Android Google Maps API教程-入门

This is android google maps api tutorial. 这是android Google Maps API教程。 In this tutorial I will teach you to integrate google maps in your android app using google maps api v2. 在本教程中,我将教您使用google ma...

culing2941的博客 895

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

iPhone用户必看:Google Maps位置历史如何在Android手机Timeline Visualizer中使用

Timeline Visualizer 是一款开源的 Android 旅行地图视频生成器,能把 Google Maps(Google 地图)Timeline 位置历史导出文件变成一段精美的动画旅行视频。对于 iPhone 用户来说有个小插曲:你的位置历史存在 Google 账户里,但这款 App 运行在 Android 手机上。好消息是,你完全可以在 iPhone 上导出位置历史,再传给 Andr

gitblog_00830的博客 852

android google maps之 简单定位(一)

前言:在android google maps之 简单定位(一)中我们已经得到世界地图了,其实我们一行代码也没有写呢,那么如果在MainActivity中加一些代码,那么我们就可以实现简单地定位了哦~~~ 1,源代码(是在之前的代码的基础上新增的代码,如果还不知道怎样得到世界地图的,请参阅http://87426628.blog.163.com/blog/static/6069361820131

fishmai的专栏 8193

真机缺少com.google.android.maps.jar解决方法:

写了个基于google map api的应用,模拟器一直都顺利,移植到真机时由于真机没有com.google.android.maps.jar包一直运行不了,直到搜到此贴。 重点在这部分: 手机终端需要支持Google Add-ons 作为与Google合作的手机终端的制造商,如果想在自己的Android终端中集成Google的一些应用,例如map,market,picasa,sync等,需要...

木叶风的博客 2075

maps google android版,google maps中文安卓版

google maps中文安卓版是一款实用非常广泛的地图,oogle maps中文安卓版可让您更轻松、更快速地探索这个大千世界,数据和地理位置非常准确,深受广大网友的喜爱!google maps中文安卓版软件介绍:谷歌地图在手机上查看google地图、卫星图、生活信、gps定位、公交驾车导航等如果没有gps,一样可以非常方便地使用这个软件,谷歌手机地图会读取你的移动电话所使用的基站(就是移动电话发...

weixin_36062835的博客 644

RGF_CJ 中 tagIMECHARPOSITION 结构体解析:Win32 输入法字符位置查询的仓颉封装

`tagIMECHARPOSITION` 是 RGF_CJ 在 `rgf.win_imm` 子包中提供的 Win32 输入法(IMM)结构体,用于在 IME 组合窗口(composition window)场景下描述“某个字符在屏幕上的位置、行高以及文档区域”,是应用响应 `WM_IME_REQUEST`(`IMR_QUERYCHARPOSITION`)请求时填写并返回给输入法的关键数据结构。阅读

gitblog_00503的博客 1066
上一篇: Android(十一):ubuntu10.10下Google Map入门
下一篇: Java编程陷阱-类成员初始化
veryitman
博客等级 码龄16年 472粉丝 310原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址