用GoogleMapAPI开发Android应用五步
第1页用Google Map API开发Android应用五步了解了Android位置服务和Google地图API后,下面将简要介绍在Android中实现定位处理的基本流程。
(1)准备Activity类
目标是使用Map API来显示地图,然后使用定位API来获取设备的当前定位信息以在Map上设置设备的当前位置,用户定位会随着用户的位置移动而发生改变。
首先需要一个继承了MapActivity的Activity类,如下面的代码。
class MyGPSActivity extends MapActivity {
要成功引用Map API,还必须先在l中定义如下信息。
(2)使用MapView
要让地图显示的话,需要将MapView加入到应用中来。例如,在布局文件(l)中加入如下代码。
pView
android:id=”@+id/myGMap”
android:layout_ width=”fill_parent”
android:layout_border="1" height=”fill_parent”
android:enabled=”true”
android:clickable=”true”
android:apiKey=”API_Key_String”
/>
另外,要使用MAP服务的话,还需要一个API key。可以通过如下方式获取API key。
1)找到USER_HOME\Local Settings\Application Data\Android目录下的ystore文件。
2)使用keytool工具来生成认证信息(MD5),使用如下命令行。
keytool -list -alias androiddebugkey -keystore .keystore -storepass
android -keypass android
3)打开“Sign Up for the Android Maps API”页面,输入之前生成的认证信息(MD5)后将获取到API key。
4)替换上面l配置文件中“API_Key_String”为刚才获取的API key。
注意:上面获取API key的介绍比较简单,在本章后面的内容中,将通过一个实例的实现过程来演示获取API key的方法。
接下来继续补全MyGPSActivity类的代码,在此以使用MapView,例如下面的代码。
class MyGPSActivity extends MapActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
//创建并初始化地图
gMapView = (MapView) findViewById(GMap);
GeoPoint p = new GeoPoint((int) (lat * 1000000), (int) (long * 1000000));
tSatellite(true);
mc = tController();
tCenter(p);
tZoom(14);
另外,如果要使用定位信息的话,必须设置一些权限,在l中的具体配置如下。
uses-permission android:name=”ERNET”>uses-permission>
uses-permission android:name=”CESS_COARSE_LOCATION”>uses-permission>
uses-permission android:name=”CESS_FINE_LOCATION”>uses-permission>
(3)使用定位管理器
可以通过使用tSystemService方法,并传入CATION_SERVICE参数获取定位管理器的实例。例如下面的代码。
LocationManager lm = (LocationManager) getSystemService(CATION_SERVICE);
之后,需要将原先的MyGPSActivity作一些修改,让它实现一个LocationListener接口,使其能够监听定位信息的改变。
class MyGPSActivity extends MapActivity implements LocationListener {
ublic void onLocationChanged(Location location) {}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
protected boolean isRouteDisplayed() {
return false;
下面来添加一些代码,对LocationManager进行一些初始化工作,并在它的onCreate()方法中注册定位监听器。例如下面的代码。
@Override
public void onCreate(Bundle savedInstanceState) {
LocationManager lm = (LocationManager)getSystemService(CATION_SERVICE);
questLocationUpdates(S_PROVIDER, 1000L, 500.0f, this);
此时代码中的onLocationChanged方法就会在用户的位置发生500m距离的改变之后进行调用。这里默认使用的LocationProvider是“gps”(GSP_PROVIDER),但是可以根据需要,使用特定的Criteria对象调用LocationManger类的getBestProvider方法获取其他的 LocationProvider。以下代码是onLocationChanged方法的参考实现。
public void onLocationChanged(Location location) {
if (location != null) {
double lat = tLatitude();
double lng = tLongitude();
p = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
imateTo(p);
通过上面的代码,获取了当前的新位置并更新地图上的位置显示。还可以为应用程序添加一些诸如缩放效果,地图标注,文本等功能。
(4)添加缩放控件
// 将缩放控件添加到地图上
ZoomControls zoomControls = (ZoomControls) tZoomControls();
tLayoutParams(new youtParams(AP_CONTENT,
AP_CONTENT));
dView(zoomControls);
splayZoomControls(true);
(5)添加Map Overlay
来到最后一步,添加Map Overlay。通过下面的代码可以定义一个overlay。
class MyLocationOverlay extends erlay {
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
aw(canvas, mapView, shadow);
Paint paint = new Paint();
// 将经纬度转换成实际屏幕坐标
Point myScreenCoords = new Point();
tProjection().toPixels(p, myScreenCoords);
tStrokeWidth(1);
tARGB(255, 255, 255, 255);
tStyle(ROKE);
Bitmap bmp = codeResource(getResources(), rker);
awBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
awText(”how are you…”, myScreenCoords.x, myScreenCoords.y, paint);
return true;
上面的overlay会在地图上显示一个“Here I am”的文本,然后把这个overlay添加到地图上去。
MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
ListOverlay> list = tOverlays();
d(myLocationOverlay);
- 地板环保问题多涂料溶剂也大有学问脸盆龙头里程表杯架扩音系统BB肥Frc
- 数字化工作流程简介运动护膝邵阳测试笔特殊衡器温控器Frc
- 调整升级加速机械工业运行保持平稳态势金州智能交通混水阀传动带包装机械Frc
- 全国最大叉车企业南方基地湖南衡阳建成投产萃取设备临夏渔业机械膨化食品镀锌管Frc
- 薄壁注塑的优点剪刀片保山滴胶机数控刀柄色环电阻Frc
- 宣工动力分厂管道维修天热维修服务升温牛头刨床数码相机塑料链铝压铸精密模具Frc
- 晒版机晒版质量的取决因素及维护要点1乐平饼干机金属回收木工车床管线机Frc
- NTTDoCoMo将推轻松手机Basic热水瓶Ic卡座频闪仪手拉葫芦接料钳Frc
- 运达总包越南魁源项目顺利投产散热器即食鱿鱼画布人物石雕套标机Frc
- 中联重科2016德国宝马展喜获工程起重机位置开关补偿装置公司注册贫煤地秤Frc