1.题记
提起谷歌Map相信大家都不会陌生,那进入我们今天的话题,如何在Android手机上进行Google Map的开发。
2.Map应用程序的开发
2.1 准备工作
2.1.1 申请Android Map API KEY
步骤一: 找到你的debug.keystore文件,在Eclipse 首选项中可以看到该文件。如下图:
![](http://dl.iteye.com/upload/attachment/437789/aba41192-54c7-34d1-8943-299d5f1968de.jpg)
步骤二:取得debug.keystore的MD5值
在命令行下进入debug.keystore文件所在的路径,执行命令:keytool -list -keystore debug.keystore,会提示输入密码,输入默认密码“android”,即可取得MD5值。
步骤三:申请Android Map的API key。
在浏览器重输入网址:http://code.google.com/intl/zh-CN/android/maps-api-signup.html,登录Google账号,输入步骤2中得到的MD5值,即可申请到API Key。记下API Key。
2.1.2 创建基于Google APIs的AVD
在Eclipse中打开AVD 界面,创建AVD,选择Target为Google APIs的项,如下图:
![](http://dl.iteye.com/upload/attachment/437970/ad8ca8dc-9ce4-3fc7-9b96-a21840deef52.jpg)
若在Target处无Google APIs选项,请自行添加maps.jar文件。
2.1.3 创建基于Google APIs的工程(略),即选择Build Target为Google APIs。
2.2 Google Map API的使用
其类均在com.google.android.maps包中,一下是该包中几个重要的类:
MapActivity用于显示Google Map的Activity类,该类为抽象类,开发时请继承该类,并实现onCreate()方法。在该类中必须创建一个MapView实例。
MapView 用户显示地图的View组件.
MapController 用于控制地图的移动、缩放等
Overlay 这是一个可显示于地图上的可绘制的对象
GeoPoint 一个包含经纬度位置的对象
2.3 实例
2.3.1 创建工程,注意选择Build Target为“Google APIs”
2.3.2 修改AndroidManifest.xml文件,增加访问网络的权限
<uses-permission android:name="android.permission.INTERNET" />
2.3.3 创建Map View,代码如下:
<com.google.android.maps.MapView
android:id="@+id/MapView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0u7spJisVnJZmy3X6nX1M01SirYWYgNm-EQZbhQ"/>
其中APIKEY即为之前得到的APIkey。
2.3.4 实现MapActivity,代码和讲解如下:
package com.sulang.android.map;
import java.util.List;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
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.Overlay;
public class Activity01 extends MapActivity
{
private MapView mMapView;
private MapController mMapController;
private GeoPoint mGeoPoint;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMapView = (MapView) findViewById(R.id.MapView01);
//设置为交通模式
//mMapView.setTraffic(true);
//设置为卫星模式
//mMapView.setSatellite(true);
//设置为街景模式
mMapView.setStreetView(false);
//取得MapController对象(控制MapView)
mMapController = mMapView.getController();
mMapView.setEnabled(true);
mMapView.setClickable(true);
//设置地图支持缩放
mMapView.setBuiltInZoomControls(true);
//设置起点为成都
mGeoPoint = new GeoPoint((int) (30.659259 * 1000000), (int) (104.065762 * 1000000));
//定位到成都
mMapController.animateTo(mGeoPoint);
//设置倍数(1-21)
mMapController.setZoom(12);
//添加Overlay,用于显示标注信息
MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
List<Overlay> list = mMapView.getOverlays();
list.add(myLocationOverlay);
}
protected boolean isRouteDisplayed()
{
return false;
}
class MyLocationOverlay extends Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
Paint paint = new Paint();
Point myScreenCoords = new Point();
// 将经纬度转换成实际屏幕坐标
mapView.getProjection().toPixels(mGeoPoint, myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 0, 0);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.home);
canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
canvas.drawText("**广场", myScreenCoords.x, myScreenCoords.y, paint);
return true;
}
}
}
2.3.5 启动模拟器,看效果图:
![](http://dl.iteye.com/upload/attachment/438268/f70cd902-a539-3e7c-82e3-0c8b78136d89.png)
至此关于Google Map的开发已完成,下面是GPS的开发。
3.GPS应用开发
3.1相关API说明
关于地理定位系统的API全部位于android.location包内,其中包括以下几个重要的功能类:
LocationManager:本类提供访问定位服务的功能,也提供了获取最佳定位提供者的功能。
LocationProvider:该类是定位提供者的抽象类。定位提供者具备周期性报告设备地理位置的功能
LocationListener:提供定位信息发生改变时的回调功能。必须事先在定位管理器中注册监听器对象。
Criteria:该类是的应用能够通过在LocationProvider中设置的属性来选择合适的定位提供者.
Geocider:用于处理地理编码和反向地理编码的类。
要使用地理定位,首先需要取得LocationManager的实例:
locationManager = (LocationManager) getSystemService(context);
取得LocationManager对象之后,还需要注册一个周期性的更新视图:
locationManager.requestLocationUpdates(provider, 3000, 0,locationListener);
其中第一个参数是设置服务提供者,第二个参数是周期。最后一个参数是用来监听定位信息的改变的。
3.2 具体实例
3.2.1 在AndroidManifest.xml文件中添加权限,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sulang.android.map" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".Activity01" 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>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
3.2.2 给模拟器设置个默认的坐标值
启动Eclipse ,选择Window ->Show View 打开 Emulator Control 界面即可进行设置。
3.2.3 实现MapActivity
具体代码和讲解如下:
package com.sulang.android.map;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
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.Overlay;
/*
*@author 七里香的悔恨,2011-3-16
*MyMapActivity.java
*Blog:[url]http://bigboy.iteye.com/[/url]
*/
public class MyMapActivity extends MapActivity {
public MapController mapController;
public MyLocationOverlay myPosition;
public MapView myMapView;
private static final int ZOOM_IN = Menu.FIRST;
private static final int ZOOM_OUT = Menu.FIRST + 1;
@Override
protected boolean isRouteDisplayed() {
return false;
}
class MyLocationOverlay extends Overlay {
Location mLocation;
// 在更新坐标时,设置该坐标,一边画图
public void setLocation(Location location) {
mLocation = location;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
Paint paint = new Paint();
Point myScreenCoords = new Point();
// 将经纬度转换成实际屏幕坐标
GeoPoint tmpGeoPoint = new GeoPoint(
(int) (mLocation.getLatitude() * 1E6), (int) (mLocation
.getLongitude() * 1E6));
mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 0, 0);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.home);
canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
canvas.drawText("Here am I", myScreenCoords.x, myScreenCoords.y,
paint);
return true;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 取得LocationManager实例
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(context);
myMapView = (MapView) findViewById(R.id.MapView01);
// 取得MapController实例,控制地图
mapController = myMapView.getController();
myMapView.setEnabled(true);
myMapView.setClickable(true);
// 设置显示模式
myMapView.setSatellite(true);
myMapView.setStreetView(true);
// 设置缩放控制
myMapView.setBuiltInZoomControls(true);
myMapView.displayZoomControls(true);
// 设置使用MyLocationOverlay来绘图
mapController.setZoom(17);
myPosition = new MyLocationOverlay();
List<Overlay> overlays = myMapView.getOverlays();
overlays.add(myPosition);
// 设置Criteria(服务商)的信息
Criteria criteria = new Criteria();
// 经度要求
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
// 取得效果最好的criteria
String provider = locationManager.getBestProvider(criteria, true);
// 得到坐标相关的信息
Location location = locationManager.getLastKnownLocation(provider);
// 更新坐标
updateWithNewLocation(location);
// 注册一个周期性的更新,3000ms更新一次
// locationListener用来监听定位信息的改变
locationManager.requestLocationUpdates(provider, 3000, 0,
locationListener);
}
private void updateWithNewLocation(Location location) {
String latLongString;
String addressString = "没有找到地址\n";
if (location != null) {
// 为绘制标志的类设置坐标
myPosition.setLocation(location);
// 取得经度和纬度
Double geoLat = location.getLatitude() * 1E6;
Double geoLng = location.getLongitude() * 1E6;
// 将其转换为int型
GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());
// 定位到指定坐标
mapController.animateTo(point);
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "经度:" + lat + "\n纬度:" + lng;
double latitude = location.getLatitude();
double longitude = location.getLongitude();
// 更具地理环境来确定编码
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
// 取得地址相关的一些信息\经度、纬度
List<Address> addresses = gc.getFromLocation(latitude,
longitude, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
addressString = sb.toString();
}
} catch (IOException e) {
}
} else {
latLongString = "没有找到坐标.\n";
}
// 显示
// myLocationText.setText("你当前的坐标如下:\n"+latLongString+"\n"+addressString);
}
private final LocationListener locationListener = new LocationListener() {
// 当坐标改变时触发此函数
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
// Provider被disable时触发此函数,比如GPS被关闭
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
// Provider被enable时触发此函数,比如GPS被打开
public void onProviderEnabled(String provider) {
}
// Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
}
至此 GPS 应用开发完毕。
源代码
![点击查看原始大小图片](http://dl2.iteye.com/upload/attachment/0043/7789/aba41192-54c7-34d1-8943-299d5f1968de-thumb.jpg)
- 大小: 70.5 KB
![点击查看原始大小图片](http://dl2.iteye.com/upload/attachment/0043/7970/ad8ca8dc-9ce4-3fc7-9b96-a21840deef52-thumb.jpg)
- 大小: 44.7 KB
![点击查看原始大小图片](http://dl2.iteye.com/upload/attachment/0043/8268/f70cd902-a539-3e7c-82e3-0c8b78136d89-thumb.png)
- 大小: 60.6 KB
分享到:
相关推荐
Android系统提供LocationManager和LocationProvider接口来获取GPS位置。在用户授予定位权限后,可以监听位置变化并更新地图上的当前位置标记。 7. **经纬度查找目的地** 经纬度坐标可以用来精确查找地图上的特定...
在Android平台上,GPS(全球定位系统)定位是开发者常用的功能之一,用于获取设备的精确位置信息。本资源提供了Android GPS定位的实例源码,对于学习和理解如何在Android应用中集成GPS定位至关重要。通过分析这些...
在Android开发中,GoogleMap API是一个非常重要的工具,它允许开发者在应用中集成地图功能,实现定位、导航、路线规划等复杂操作。本篇将详细探讨如何在Android应用中使用GoogleMap进行定位。 首先,我们需要在项目...
<uses-feature android:name="android.hardware.location.gps" /> <uses-feature android:name="android.hardware.location.network" /> ``` 为了获取位置信息,你可以使用`FusedLocationProviderClient`,它是...
<uses-feature android:name="android.hardware.location.gps" android:required="false" /> <uses-feature android:name="android.hardware.location.network" android:required="false" /> android:name=...
在Android开发中,涉及到地图应用时,经常需要处理不同坐标系之间的转换,特别是GPS坐标与地图服务提供商坐标之间的转换。本文主要聚焦于Android系统中如何将GPS获取的坐标转换为适用于高德地图的坐标。 首先,我们...
Android 高德地图gps定位.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
"Android Map"是指利用Google Maps API或者其他的地图服务提供商在Android应用中集成地图展示、位置定位、导航等功能的技术。本篇文章将深入探讨Android地图开发的关键知识点。 1. **Google Maps API**: Google ...
实现通过GPS或NetWork获取当前位置的经纬度,并且在Google Map Android v2上显示
**Android-RunMap:一个简单的运动跑步App** RunMap是一款基于Android平台的简单运动跑步应用,旨在为用户提供记录跑步轨迹、统计运动数据等基本功能。本文将深入探讨Android开发技术,特别是如何构建这样一个完整...
开发者可以通过解压并导入到Android Studio来研究一个实际的GPS应用,了解其工作原理,包括如何创建地图标记、显示用户轨迹,以及如何利用Google Maps API或OpenStreetMap API进行地图集成。 综上所述,这个...
在本项目中,"基于Google Map的GPS轨迹定位系统"是一个综合性的技术实施,它结合了全球定位系统(GPS)的技术与Google Maps的接口,旨在实现对移动目标的实时跟踪和定位。这样的系统广泛应用于物流、交通管理、紧急...
在Android平台上,开发一款GPS定位器程序涉及到许多关键知识点,这些知识点构成了Android应用程序与GPS硬件交互的基础。首先,我们来详细探讨一下这些重要的技术点。 1. **AndroidManifest.xml配置**: 在Android...
关于GPSTest,这是一个常见的Android GPS测试应用的名字,它通常用于测试设备的GPS接收器功能。在Android中,你可以使用LocationManager服务来获取GPS位置数据。注册一个LocationListener,当GPS位置改变时,...
安卓源码包 Android GPS 开发 地图&导航&定位&指南 45个合集: AMap_Android_API_Demo_V2.0.4(Location_API_V1.0.2).zip Android 4.0下指南针开发源码,可在Nexus 4上完美运行.zip Android GPS 开发client端代码分享....
在Android平台上,Google Map是一个非常重要的工具,它不仅提供了丰富的地图信息,还支持定位、导航、路线规划等功能。本文将详细介绍如何在Android应用中集成Google Map,并实现定位功能。 首先,要在Android应用...
在AndroidManifest.xml文件中,需要添加必要的权限声明,如访问网络、读写外部存储以及使用GPS定位等。例如,`<uses-permission android:name="android.permission.INTERNET"/>`用于网络访问,`<uses-permission ...
通过以上步骤,你就可以创建一个简单的Android Map程序,它能显示地图,并利用GPS定位功能在地图上标出用户的位置。然而,实际应用可能需要更多的功能,如路线规划、地理编码和反编码、多点标记等,这些可以通过...
在Android开发中,集成Google地图并实现地图图层叠加是一项常见的任务,这使得开发者能够创建出功能丰富的地理位置应用。Google地图API提供了强大的功能,允许我们不仅显示基础的地图数据,还能添加自定义图层,比如...