Android Map新用法:MapFragment

移动开发 Android
Google Maps Android API 在2012年12月更新,其V2版摒弃了MapActivity,而采用MapFragment,从而可以更好的在Android中使用地图组件。

1.MapView ,MapActivity 这种的局限在于,必须要继承MapActivity,否则无法使用MapView。纠结就在于此。但是,***官网上已经弃用了这糟粕的MapActivity。

Version 1 of the Google Maps Android API as been officially deprecated as of December 3rd, 2012. This means that from March 3rd, 2013 you will no longer be able to request an API key for this version. No new features will be added to Google Maps Android API v1. However, apps using v1 will continue to work on devices. Existing and new developers are encouraged to use Google Maps Android API v2.

MapFragment是刚出的google官方包,要集成进来还是挺麻烦的。官网链接

  • 首先要到google conlose 添 加api access权限,拿到apikey,创建一个project ,然后到services里把Google Maps Android API v2打开,再到api access里把你的project的keystore的SHA1和包名填进去,拿到唯一的api key
  •  打开sdk manager--在Extras里把Android Support Libaray 和 Google Play Services都安装,lib和samples都在sdk_path/extra/google/google_play_services下。导 入../lib_project作为自己项目的libaray
  • 这一步完了之后在manifest.xml文件里添加权限和api key。代码如下:
  1. <permission 
  2.         android:name="com.example.permission.MAPS_RECEIVE" 
  3.         android:protectionLevel="signature"/> 
  4. com.example替换成自己project的package 
  5.     <uses-permission android:name="com.example.permission.MAPS_RECEIVE"/> 
  6.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
  7.     <uses-permission android:name="android.permission.INTERNET"/> 
  8.     <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 
  9.     <!--My Location--> 
  10.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
  11.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
  12.     <!--Maps API needs OpenGL ES2.0.--> 
  13.     <uses-feature 
  14.         android:glEsVersion="0x00020000" 
  15.         android:required="true"/> 
  16. 放在application声明里 
  17.     <meta-data 
  18.             android:name="com.google.android.maps.v2.API_KEY" 
  19.             android:value="appkey"/> 

在layout xml里加入MapFragment声明:

  1. <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
  2.   android:id="@+id/map" 
  3.   android:layout_width="match_parent" 
  4.   android:layout_height="match_parent" 
  5.   class="com.google.android.gms.maps.MapFragment"/> 

activity不变,extends Activity就可以了。 不需要继承MapActivity了。

注意,debug状态下是不可以显示地图的,一定要在正式签名下map才会显示出来。

install下应该就能看到map了。

还有不懂的就多看看官方文档把,介绍的很清楚。

责任编辑:徐川 来源: cnblogs
相关推荐

2018-06-29 09:52:45

2009-11-03 17:14:32

无线接入网技术

2021-12-07 05:44:45

Vue 3 Watch WatchEffect

2010-04-14 14:06:40

当做无线交换机

2009-07-27 19:55:15

2023-06-14 08:54:09

Map方法ForEach方法

2021-12-08 09:09:33

Vue 3 Computed Vue2

2020-04-27 20:55:42

JavaJava 8编程语言

2018-03-05 11:05:26

2013-01-08 17:30:31

Google MapsAndroid MapMapFragment

2010-01-26 10:02:51

Android But

2014-06-19 10:43:37

AndroidScrollerView平滑滚动

2011-05-31 16:50:35

Android 线程

2010-10-25 09:06:47

Google Map应用

2021-09-15 16:05:41

map.putJavaMap

2012-04-17 14:25:05

Chrome OSAura 体验

2014-12-30 15:33:22

Gmail邮箱

2010-10-12 17:08:16

MySQL命令行

2020-12-07 13:48:48

EditorAndroid开发者

2016-08-31 13:48:00

AndroidRetrofit源码解析
点赞
收藏

51CTO技术栈公众号