`
zhoujinhuang
  • 浏览: 91838 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

day2 学习定位+地图API

阅读更多

任务是做一个定位+手机拍照+地图的很常见的应用

今天按官方教程做了一个定位+地图的例子了解一下API。拍照的例子没找到官方的例子,瞎搜了一圈例子没做完,明天再整理。

定位、地图相关的配置:

AndroidManifest.xml

	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<uses-library android:name="com.google.android.maps" />
		<activity android:name=".LocationMapActivity" 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_FINE_LOCATION" />
	<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

 主要是声明 需要googlemap的lib和互联网和定位的访问权限。

 布局中需要声明MapView

 <com.google.android.maps.MapView
  android:id="@+id/mapview" android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:clickable="true"
  android:apiKey="0MhdDulsEpJMaz65SIdleweZoylEaKL11yQc_nQ" />

 关键的地方在apiKey,网上一堆因为这个访问不到地图,问问题的。 apiKey是与应用的签名证书关联的,使用google地图需要在,在http://code.google.com/intl/zh-CN/android/maps-api-signup.html 注册。完整说明见http://code.google.com/intl/zh-CN/android/add-ons/google-apis/mapkey.html

这里只是获得一个与debug密钥匹配的签名。

1 先从debug的密钥库取出密钥

密钥库一般在 C:\Documents and Settings\<user>\.android\debug.keystore
执行命令输入签名

keytool -list -alias androiddebugkey -keystore debug.keystore -storepass android -keypass android

结果类似

Certificate fingerprint (MD5): 94:1E:43:49:87:73:BB:E6:A6:88:D7:20:F1:8E:B5:98 


2 在http://code.google.com/intl/zh-CN/android/maps-api-signup.html进行注册
获取map api key


 

主要的代码:

定位主要的API 是位置服务的访问入口 LocationManager  和 位置服务时间的侦听器LocationListener 。

通过LocationManager访问服务:

 

		LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000,
				10, myLocationListener);
		locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10,
				myLocationListener);

位置服务有多个Provider,一般是GPS 和 基站定位的,没搞清电信服务商没开放接口的话,如何通过API如何能访问得到基站位置数据。。。

 

 通过LocationListener 接受事件和位置信息

	public final LocationListener myLocationListener = new LocationListener() {
		//位置变化事件
		@Override
		public void onLocationChanged(Location location) {
			GeoPoint point = new GeoPoint(
					(int) (location.getLatitude() * 1000000), (int) (location
							.getLongitude() * 1000000));
			OverlayItem overlayitem2 = new OverlayItem(point, "title",
					"snippet2");
			itemizedOverlay.addOverlay(overlayitem2);
			mapView.getController().animateTo(point);
			Log.i(this.getClass().getName(), "---location:"
					+ location.toString());
		}
		//用户关闭位置服务Provider
		@Override
		public void onProviderDisabled(String provider) {
			Log.i(this.getClass().getName(), "---onProviderDisabled:"
					+ provider.toString());
		}
		//用户启用位置服务Provider可用,例如连到GPS卫星信号
		@Override
		public void onProviderEnabled(String provider) {
			Log.i(this.getClass().getName(), "---onProviderEnabled:"
					+ provider.toString());

		}
		//位置服务Provider状态变化,例如连到GPS卫星信号
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			Log.i(this.getClass().getName(), "---onStatusChanged:" + status);
		}
	};

 

地图代码:

 

		linearLayout = (LinearLayout) findViewById(R.id.zoomview);
		//获取mapView
		mapView = (MapView) findViewById(R.id.mapview);
		//设置Zoom 
		mapView.getController().setZoom(13);
		//zoom的显示控制按钮
		mapView.setBuiltInZoomControls(true);
		//获取地图上的图层
		mapOverlays = mapView.getOverlays();
		//增加一个层,后续在地图上加标识
		drawable = this.getResources().getDrawable(R.drawable.icon);
		itemizedOverlay = new HelloItemizedOverlay(drawable);
		mapOverlays.add(itemizedOverlay);

 

 

 

分享到:
评论
2 楼 zgycsmbi 2012-01-14  
楼主,能把完整的代码发一份给我吗
350038760@qq.com
thanks
1 楼 rmn190 2010-10-15  
能否把完整的代码传上来呢?

相关推荐

Global site tag (gtag.js) - Google Analytics