【Android·GoogleMap】Google Maps API V2 的显示

Google Maps API 已经从V1更新到V2版本了,而很多书本都还是V1版本的,最近刚好在弄Google Map 的应用。所以,将一些Google Maps API V2使用过程及过程中的一些问题作一些记录。


1、申请Map API Key

想要在Android应用中显示Google Map 数据,必须注册Google Map 服务,获得Maps API Key.

要注册一个Maps API Key,需要提供一个对应程序进行签名的证书的MD5指纹。

在Eclipse中的Default debug keystore的路径。选择Winddows→Preferences,选择Android,单击Build,记住Default debug keystore 的路径。


在CMD进入命令行,输入命令:

Keytool -list -alias androiddebugkey -keystore "C:\Users\ThinkPad User\.android\debug.keystore" -storepass android -keypass android

注:双引号内的路径要和上图的路径一致。

输入命名后就会出线证书指纹,而我们要把SHA1复制下来,用于申请Key


因为在Google 在大陆已经被屏蔽了,需要翻墙,在开着翻墙软件的情况下,浏览器中打开https://code.google.com/apis/console/  。当然还要有Google 的账号,没有就申请一个。

        Create project

   创建工程之后,进入工程里,点击左侧的APIs&auth下属的APIs ,将Google Maps Android APIs v2 打开



申请Key,点击APIs&auth下属的Credentials,再点击右边的Create new key,将我们在上面得到的SHA1复制进去,然后是分号隔开,后面跟的是包名,这个包名是非常重要的,我们将要建的应用,就是要用这个包名的。


之后,点击Create 就可以看到生成的key

2.创建工程

记得创建工程时packege name 一定要和申请key的包名一样


Android SDK Manger中下载支持库和Google play services


在Eclipse里面选择:File > Import > Android > Existing Android Code Into Workspace然后点击Next.

  之后Browse..., 找到路径下的<android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib, 记得一定要把Add project into workspace 勾上,要不然会出现错误的。然后选择Finish。

在自己的项目上右键,选Properties,左边选Android,然后在下面的Library里面Add刚才的google-play-services_lib。


3、项目代码

想要是用GPS定位,需要在AndroidManifest.xml文件中添加权限。

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mygooglemap"
    android:versionCode="1"
    android:versionName="1.0" >
    
     <!-- 增加一些许可权限 -->
     <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <permission
          android:name="com.example.mygooglemap.permission.MAPS_RECEIVE"
          android:protectionLevel="signature"/>
        <uses-permission
             android:name="com.example.mygooglemap.permission.MAPS_RECEIVE"/>
        <uses-permission android:name="android.permission.INTERNET"/>
		<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
		
		<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
		<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
		<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
		
		<!-- 写sd卡 google地图会往sd卡上写缓存数据-->
		<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
		
		
		
		<!-- OpenGL ES V2特性支持 -->
		<uses-feature 
			  android:glEsVersion="0x00020000" 
			  android:required="true"/>

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
         <!-- Google map key -->
         <meta-data
		    android:name="com.google.android.maps.v2.API_KEY"
		    android:value="YOUR KEY"/>
      
        <meta-data   
          android:name="com.google.android.gms.version"  
          android:value="@integer/google_play_services_version"/> 
      
        <!--  
        <uses-library android:name="com.google.android.maps" />
		-->
    </application>

</manifest>
注:将相关的package,YOUR KEY换成自己的。

Layout文件

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mygooglemap.MainActivity" >

    	
	<LinearLayout
	    android:id="@+id/linearlayout"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:orientation="horizontal" >
	    
	    <TextView    
	    android:id="@+id/text"    
		android:layout_width="wrap_content"        
		android:layout_height="wrap_content"        
		android:text="@string/hello_world" />
	    
	  <Button
	    android:id="@+id/button1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="普通视图" 
	    android:onClick="NormolView"/>

	<Button
	    android:id="@+id/button2"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="卫星视图" 
	    android:onClick="Satellite"/>
	    
	</LinearLayout>

	<fragment
	    android:id="@+id/map"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	   android:layout_below="@+id/linearlayout"
	    class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>

MainActivity.java文件

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

import android.app.Activity;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    private GoogleMap googleMap;   
	
	@Override     
	protected void onCreate(Bundle savedInstanceState) {         
	super.onCreate(savedInstanceState);         
	setContentView(R.layout.activity_main);  
	
	// 获取地图  
	googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
	googleMap.setMyLocationEnabled(true);

	//地图样式的切换
		Button btn1 = (Button) findViewById(R.id.button1);
		Button btn2 = (Button) findViewById(R.id.button2);
		
	btn1.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v) {
		googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		}
	});
	btn2.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v) {
		googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
		}
	});
	}
}



4.创建虚拟机

创建虚拟机可以有两种

一、是创建自带Google play services 的虚拟机,但是一定是要Google inc level 19以上的



二、是创建普通的虚拟机,但是要自己装google play 和google play services

普通的虚拟机中安装google play 和google play services,这两个apk可以去豌豆荚上下载,然后放入到 .android的目录下,在开着虚拟机的情况下,在CMD命令中进入 .android文件中,命令执行adb install GooglePlay.apk 和 adb install GooglePlayservices.apk

如果adb命令执行不了,就去sdk 下面的platform-tools 文件中将关于adb的这几个文件复制到 .android文件中,CMD中直接运adb,出现下面图片中情况,则说明可以运行adb命令了




最后运行程序,就可以看到Google Map 了









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值