android Google map 范例
来源:互联网 发布:布料淘宝 编辑:程序博客网 时间:2024/05/29 17:05
忽略掉Google map key申请,直接总结mapView的使用。这里我总结了两个:
一、在Google地图上显示本地的位置。
1、首先注意在AndroidManifest.xml文件中,增加权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
在application中activity外,增加:uses-library
<uses-library android:name="com.google.android.maps" />
2、在类中,将extends Activity改为extends MapActivity
3、设置可放大缩小地图的控件。
在xml文件中,添加以下内容:
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/zoom" android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" >
</LinearLayout>
在类中,加入:
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mapView.getController().setZoom(14);//设置缩放级别
p = this.getCurrentGeoPoint();
mapView.getController().animateTo(p);// 通过动画方式移动到指定坐标s
4、通过以下方法得到当前位置的经纬度
private GeoPoint getCurrentGeoPoint() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
return new GeoPoint((int) (location.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6));
}
5、通过以下方式显示当前位置在地图上显示:
class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//—translate the GeoPoint to screen pixels—
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//—add the marker—
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.pushpin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
在oncreate方法中,加入:
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
这样就能在屏幕中显示你当前位置的地图显示了;
如下图:
源代码见:http://henzil.googlecode.com/svn/trunk/android.googleMap01/
二、输入地址,在地图上显示相应的位置。此方法是通过查询地址名称,返回一个list结果。在地图上显示:
此方法与上述方法基本一致,不同之处在于:是用getFromLocationName方法,来查询目的地的经纬度。
如下代码:
// 通过系统默认区域设置进行地图定位
Geocoder gc = new Geocoder(this);
mapView.setStreetView(true);
try {
// 通过地址名称描述返回一个查询结果的数组(后面参数为返回最大结果数)
addresses = gc.getFromLocationName(address, 5);
// 如果未查询到任何结果
if (addresses != null) {
geoPoint = new GeoPoint(
// 返回纬度,经度
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
setTitle(addresses.get(0).getFeatureName());
MyOverlay myOverlay = new MyOverlay();
mapView.getOverlays().add(myOverlay);
mapView.getController().setZoom(16);
mapView.getController().animateTo(geoPoint);
}
} catch (Exception e) {
e.printStackTrace();
}
如下图所示:
- android Google map 范例
- android GOOGLE MAP应用
- android google map
- android google map 气泡
- Android调用Google Map
- 使用Android Google Map
- Android Google map使用
- Android:Google Map学习
- Android Google Map
- Android google map
- android google map开发
- android google map
- android google map v2
- Android调用Google Map
- android google map
- android google map v2
- Android:Google Map API
- android google map
- 一个有用的视频播放器VLC
- c++中的时间处理
- Archlinux网络配置
- Android游戏引擎-AndEngine(整理)
- Android开发环境搭建
- android Google map 范例
- 程序员规划
- php 图片转成数据流发送
- Android上传文件到Web服务器,PHP接收文件(一)
- Notes-10:lua binder另一只轮子成员函数
- sicily 1795
- 将d:\java目录下的所有.java文件复制到d:\jad目录下,并将原来文件的扩展名从.java改为.jad
- KVM(Kenerl-based virtual machine)安装包分析研究
- java 语言规范