通过googlemap地图实现定位,以及卫星和地图。

第一,先要创建一个属于自己的Android地图API密钥,如下

打开系统运行cmd,进入DOS界面,然后输入cd .android

然后输入keytool -list -alias androiddebugkey -keystore debug.keystore

-list:打印显示证书MD5指纹
-alias 密钥库内生成MD5指纹的密钥别名
--keystore 目标密钥所在的密钥库

第二,在http://code.google.com/android/maps-api-signup.html里面输入认证认证指纹 (MD5)

认证指纹 (MD5): 04:A2:25:80:40:A9:4D:C3:A8:AF:4D:FC:08:27:80:AC

就会获取Android 地图API密钥。

第三,在主布局文件中设置google地图

                <com.google.android.maps.MapView
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"

<-- apiKey就是上面获取的Android 地图API密钥 -->
                 android:apiKey="0r3qSWTsLVP_0VfUDmBjpARWtm3hRsTPQgDrGCg"
                 />

第四,完成这些后,在AndroidManifest.xml中加入权限

//第一个为网络访问权限

//第二个为接受权限

//第三个为发送权限

<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"/>

完成上述这么多就可以看主文件了。

View Code
  1 public class CurrentLocationWithMap extends MapActivity {
2
3 MapView map;
4 ZoomControls zoom;
5 MapController ctrlMap;
6 Button inBtn;
7 Button outBtn;
8 ToggleButton switchMap;
9 //是否在地图上绘制导航线路
10 @Override
11 protected boolean isRouteDisplayed() {
12 return false;
13 }
14
15 @Override
16 public void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.main);
19
20 map = (MapView)findViewById(R.id.myMapView);
21 map.setBuiltInZoomControls(true);
22 //openGPSSettings();
23 //获取MapView的Overlay(标记地图位置)链表
24 List<Overlay> overlays = map.getOverlays();
25 //在当前位置绘制闪烁的蓝点的Overlay
26 MyLocationOverlay myLocation = new MyLocationOverlay(this,map);
27 //尝试通过位置服务来获取当前的位置
28 myLocation.enableMyLocation();
29 overlays.add(myLocation);
30 //获取一个MapController对象,来实现缩放、移动
31 ctrlMap = map.getController();
32 inBtn = (Button)findViewById(R.id.in);
33 outBtn = (Button)findViewById(R.id.out);
34 switchMap = (ToggleButton)findViewById(R.id.switchMap);
35
36 OnClickListener listener = new OnClickListener() {
37 @Override
38 public void onClick(View v) {
39 switch (v.getId()) {
40 case R.id.in:
41 //放大一个等级
42 ctrlMap.zoomIn();
43 break;
44 case R.id.out:
45 //缩小一个等级
46 ctrlMap.zoomOut();
47 break;
48 default:
49 break;
50 }
51 }
52 };
53 //注册监听者
54 inBtn.setOnClickListener(listener);
55 outBtn.setOnClickListener(listener);
56
57 //=======================================
58
59 switchMap.setOnCheckedChangeListener(new OnCheckedChangeListener() {
60 @Override
61 public void onCheckedChanged(CompoundButton cBtn, boolean isChecked) {
62 if (isChecked == true) {
63 //卫星视图开
64 map.setSatellite(true);
65 /*
66 * //交通视图
67 map.setTraffic(true);
68 //街景视图
69 map.setStreetView(true);(已过时)*/
70 } else {
71 map.setSatellite(false);
72 }
73 }
74 });
75
76 LocationManager locationManager;
77 String context = Context.LOCATION_SERVICE;
78 locationManager = (LocationManager)getSystemService(context);
79 // String provider = LocationManager.GPS_PROVIDER;
80
81
82 Criteria criteria = new Criteria();
83 criteria.setAccuracy(Criteria.ACCURACY_FINE);
84 criteria.setAltitudeRequired(false);
85 criteria.setBearingRequired(false);
86 criteria.setCostAllowed(true);
87 criteria.setPowerRequirement(Criteria.POWER_LOW);
88 //true为已经打开的provider、false为所有的provider
89 String provider = locationManager.getBestProvider(criteria, true);
90 System.out.println(provider);
91
92 Location location = locationManager.getLastKnownLocation(provider);
93 updateWithNewLocation(location);
94 locationManager.requestLocationUpdates(provider, 2000, 5,locationListener);
95 }
96 private final LocationListener locationListener = new LocationListener() {
97 public void onLocationChanged(Location location) {
98 updateWithNewLocation(location);
99 }
100 public void onProviderDisabled(String provider){
101 updateWithNewLocation(null);
102 }
103 public void onProviderEnabled(String provider){ }
104 public void onStatusChanged(String provider, int status,
105 Bundle extras){ }
106 };
107 /*
108 private void openGPSSettings() {
109 LocationManager alm = (LocationManager) this
110 .getSystemService(Context.LOCATION_SERVICE);
111 if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
112 Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
113 return;
114 }
115
116 Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
117 Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
118 startActivityForResult(intent,0); //此为设置完成后返回到获取界面
119
120 }
121 */
122 private void updateWithNewLocation(Location location) {
123 String latLongString;
124 TextView myLocationText;
125 myLocationText = (TextView)findViewById(R.id.myLocationText);
126 if (location != null) {
127 double lat = location.getLatitude();
128 double lng = location.getLongitude();
129 latLongString = "纬度:" + lat + "\n经度:" + lng;
130 /*移动到指定的纬度
131 * GeoPoint为不可变类,表示一对经、纬度值,以微度的整数形式存储*/
132 ctrlMap.animateTo(new GeoPoint((int)(lat*1E6),(int)(lng*1E6)));
133 } else {
134 latLongString = "无法获取地理信息";
135 }
136 myLocationText.setText("您当前的位置是:\n" +
137 latLongString);
138
139 }
140 }

 

posted @ 2011-11-25 16:09  WangWeiDa  阅读(424)  评论(0编辑  收藏  举报