Google map v2在android中的应用

以后如果要开发包含google map的程序就更麻烦咯 
http://www.cnblogs.com/mengdd/archive/2013/01/01/2841390.html  

找到篇不错的文章,大家可以参考一下 
http://blog.sina.com.cn/s/blog_c2839d410101jahv.html  

1. 创建Android工程 
由于v2的key需要Google Play Service的支持,所以创建的工程需要将其引入 

2. 获得keystore的SHA-1的fingerprint 
%JAVA_HOME%\bin\keytool -list -v -keystore .android\debug.keystore 
(Release时换成相应的keystore) 

3. 获得Google Map API Key 
打开 https://code.google.com/apis/console 并登录(第一次登录会提示创建API Project) 
导航栏点击Services,找到Google Maps Android API v2,设置成ON(设置后左侧导航栏的内容会增加) 
导航栏点击API Access -> Create new Android Key 
输入SHA-1的fingerprint和工程的包名,用分号分隔 
点击Create后就可以获得Key了(会获得2个Key,一个是对应Android设备的,另一个是对应Browser的) 

4. 修改AndroidManifest.xml 
添加Permission 
Xml代码   收藏代码
  1. <permission  
  2.     android:name="<your package name>.permission.MAPS_RECEIVE"  
  3.     android:protectionLevel="signature" />  
  4.   
  5. <uses-permission android:name="<your package name>.permission.MAPS_RECEIVE" />  
  6. <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />  
  7. <uses-permission android:name="android.permission.INTERNET" />  
  8. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
  9. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  

添加API Key 
Xml代码   收藏代码
  1. <application  
  2.     android:allowBackup="true"  
  3.     android:icon="@drawable/ic_launcher"  
  4.     android:label="@string/app_name"  
  5.     android:theme="@style/AppTheme" >  
  6.   
  7. ...  
  8.   
  9.     <meta-data  
  10.         android:name="com.google.android.maps.v2.API_KEY"  
  11.         android:value="<your API Key>" />  
  12. </application>  


5. 在layout.xml上添加Google Map 
Xml代码   收藏代码
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <fragment  
  8.         android:id="@+id/map"  
  9.         android:name="com.google.android.gms.maps.SupportMapFragment"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="fill_parent" />  
  12.   
  13. </RelativeLayout>  


6. 控制Google Map 
由于使用了Fragment,所以要继承FragmentActivity 
Java代码   收藏代码
  1. public class MainActivity extends FragmentActivity implements  
  2.         OnMapLongClickListener, OnMyLocationChangeListener {  
  3.   
  4.     private GoogleMap map;  
  5.     private MarkerOptions destinationMarker;  
  6.     private LatLng myLatLng;  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.   
  13.         Fragment fragmentMap = getSupportFragmentManager().findFragmentById(  
  14.                 R.id.map);  
  15.         if (fragmentMap != null)  
  16.             map = ((SupportMapFragment) fragmentMap).getMap();  
  17.   
  18.         if (map != null) {  
  19.             map.setMyLocationEnabled(true);  
  20.             map.setOnMyLocationChangeListener(this);  
  21.             map.setOnMapLongClickListener(this);  
  22.   
  23.             // location lat lng  
  24.             LatLng latLng = new LatLng(00);  
  25.             // add mark  
  26.             destinationMarker = new MarkerOptions();  
  27.             destinationMarker.position(latLng);  
  28.             destinationMarker.title("test");  
  29.             map.addMarker(destinationMarker);  
  30.             // move camera  
  31.             map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 12f));  
  32.         }  
  33.     }  
  34.   
  35.     @Override  
  36.     public boolean onCreateOptionsMenu(Menu menu) {  
  37.         // Inflate the menu; this adds items to the action bar if it is present.  
  38.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  39.         return true;  
  40.     }  
  41.   
  42.     @Override  
  43.     public void onMyLocationChange(Location loc) {  
  44.         myLatLng = new LatLng(loc.getLatitude(), loc.getLongitude());  
  45.     }  
  46.   
  47.     @Override  
  48.     public void onMapLongClick(LatLng point) {  
  49.         map.clear();  
  50.         destinationMarker = new MarkerOptions();  
  51.         destinationMarker.position(point);  
  52.         map.addMarker(destinationMarker);  
  53.     }  
  54. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值