谈Android中GoogleMap的使用(三)
来源:互联网 发布:myeclipse可视化编程 编辑:程序博客网 时间:2024/05/21 11:04
有了上一篇博文介绍的基础知识了,下面让我们看下如何用GoogleMap定位和绘图。 在上一篇博文的代码中有这样一个函数centerOnGPSPosition();我们写一下这个函数,让它实现定位功能。
private void centerOnGPSPosition() {
Log.d(TAG, "centerOnGPSPosition");
String provider = "gps";
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
MyLocationListener locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location loc = lm.getLastKnownLocation(provider);
GeoPoint mDefPoint;
mDefPoint = new GeoPoint((int) (loc.getLatitude() * 1000000), (int) (loc.getLongitude() * 1000000));
}
//MyLocationListener
protected class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
Log.d(TAG, "MyLocationListener::onLocationChanged..");
}
}
在上面的函数中我们首先定义了一个LocationManager类的变量lm,LocationManager 是一个定位管理类,负责定位功能的初始化、设置、启动以及关闭等。但是不是仅仅有了这些语句就可以实现定位,我们还要设置监听器,locationListener就是这样一个监听器。然后我们通过LocationManager类requestLocationUpdates方法更新位置信息。
我们有必要详细说明一下这个函数的几个参数,第一个参数是你所提供的provider,至于provider(包括 Location loc = lm.getLastKnownLocation(provider);也用到了)到底是什么详情请见牛人博客:http://blog.csdn.net/CBirdLG/archive/2010/10/13/5939300.aspx
第二个和第三个参数分别是你想跟新GPS信息的时间间隔和距离间隔,比如如果我要每隔1000ms和和每经过10米更新一次则将上述函数写成lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,10, locationListener);第四个参数是自己定义的一个监听器类,每当GPS信息更新则执行onLocationChanged方法。
最后我们通过定义一个Location类型的变量loc来获取经纬度坐标,然后以以前介绍过的GeoPoint表达出来。
这样基本GPS定位功能就已经完成了。
下面我们来说说画图。光得到了经纬度的坐标值看起来很不直观,我们希望能将其直观的现实在GoogleMap上。
我们在上面的onLocationChanged函数上添加以下内容
public void onLocationChanged(Location loc) {
Log.d(TAG, "MyLocationListener::onLocationChanged..");
if (loc != null) {
mOverlayController = mMapView.createOverlayController();
mDefPoint = new GeoPoint((int) (loc.getLatitude() * 1000000),
(int) (loc.getLongitude() * 1000000));
mDefCaption = "Lat:" + loc.getLatitude() + "Lng:" + loc.getLongitude();
MyOverlay mo = new MyOverlay();
mMapView.getOverlays().add(mo);
}
}
protected class MyOverlay extends Overlay {
@Override
public void draw(Canvas canvas, MapView mv, boolean shadow) {
Log.d(TAG, "MyOverlay::darw..mDefCaption=" + mDefCaption);
super.draw(canvas, mv, shadow);
if (mDefCaption.length() == 0) {
return;
}
Paint p = new Paint();
int[] scoords = new int[2];
int sz = 5;
// 转换为屏幕上的位置
Point myScreenCoords = new Point();
mMapView.getProjection().toPixels(mDefPoint, myScreenCoords);
// mMapView.set
// mv.getPointXY(mDefPoint, scoords);
// 在屏幕上绘制点
scoords[0] = clickX = myScreenCoords.x;
scoords[1] = clickY = myScreenCoords.y;
p.setTextSize(14);
p.setAntiAlias(true);
int sw = (int) (p.measureText(mDefCaption) + 0.5f);
int sh = 25;
int sx = scoords[0] - sw / 2 - 5;
int sy = scoords[1] - sh - sz - 2;
RectF rec = new RectF(sx, sy, sx + sw + 10, sy + sh);
p.setStyle(Style.FILL);
p.setARGB(128, 255, 0, 0);
canvas.drawRoundRect(rec, 5, 5, p);
p.setStyle(Style.STROKE);
p.setARGB(255, 255, 255, 255);
canvas.drawRoundRect(rec, 5, 5, p);
canvas.drawText(mDefCaption, sx + 5, sy + sh - 8, p);
// Draw point body and outer ring
p.setStyle(Style.FILL);
p.setARGB(88, 255, 0, 0);
p.setStrokeWidth(1);
RectF spot = new RectF(scoords[0] - sz, scoords[1] + sz, scoords[0]
+ sz, scoords[1] - sz);
canvas.drawOval(spot, p);
p.setARGB(255, 255, 0, 0);
p.setStyle(Style.STROKE);
canvas.drawCircle(scoords[0], scoords[1], sz, p);
}
}
我们先来看onLocationChanged函数,函数的后两句是将一个绘图添加到了地图上。其中,mo是自定义的一个类,完成图形的绘制,再通过add函数将其添加。至于MyOverlay,其作用就是将字符串mDefCaption画在地图上并为其设置一个边框。语句的具体的含义囿于篇幅不详细说明了,可参考有关Canvas编程的相关文章。
- 谈Android中GoogleMap的使用(三)
- 谈Android中GoogleMap的使用(一)
- 谈Android中GoogleMap的使用(二)
- android中使用GoogleMap的地理位置服务
- Android--GoogleMap的使用
- android 使用 googleMap
- android googleMap使用
- GoogleMap的使用
- GoogleMap的基本使用
- 关于android使用GoogleMap API V2的使用问题
- Android使用googleMap api v2
- android之GoogleMap(二)
- android GoogleMap定位(二)
- android GoogleMap定位(一)
- GoogleMap----android篇(1)
- GoogleMap----android篇(2)
- googlemap 使用
- 解决android中googlemap无法显示问题
- 用小偷程序做成的站,怎样登陆后台
- sgu 171. Sarov zones
- 自动模拟键盘和鼠标
- Integrity Real-time Operating System
- ASP.NET页面局部刷新
- 谈Android中GoogleMap的使用(三)
- 留言板如何分页显示
- mount错误汇总
- (集合上的动态规划)最有矩阵链乘模板!!!!
- poj 2513 并查集+trie,欧拉图
- C#抽象类与接口的区别
- Extjs之Eclipse及spket的开发环境的搭建
- JSP的简介与基本语法
- [Review]OSPF PE-CE Routing