Google Map在开发板上的应用

       闲来没事,一直在鼓捣公司的开发板,突然有一天想把google地图给整进来,于是开始行动。整个过程归纳为以下几个步骤:

1、基于Google API新建一个名为GoogleMap的工程

       几个重要工程文件的内容如下:

       1)、布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

   	<com.google.android.maps.MapView
   	    android:id="@+id/map_view"
   	    android:layout_width="fill_parent"
   	    android:layout_height="fill_parent"
   	    android:clickable="true"
   	    android:apiKey="0vcWNfFdVG-uJZSMXPL0BnGQGlBYAxTs3frQpfQ"
   	    />

</LinearLayout>
这里有两个地方需要说明:

i、MapView:跟Android SDK中的其它View是一样的,不同的是SDK中没有包含这个View,位于Google另外提供的jar包中,这个jar包全名叫com.google.android.maps.jar。

ii、android.apiKey:这个需要到网上进行申请,后面有说明

     2)、AndroidManifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.raycommtech.googlemap"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <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"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <uses-library android:name="com.google.android.maps"/>
        <activity
            android:label="@string/app_name"
            android:name=".GoogleMapActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
有三个地方需要说明

i、android:minSdkVersion默认是14,我板子上的系统是2.3.7,API的Level是10,因此要改成目标板系统的API Level,否则运行时报以下错误:

[2012-02-22 13:05:56 - GoogleMap] ERROR: Application requires API version 14. Device API version is 10 (Android 2.3.7).
[2012-02-22 13:05:56 - GoogleMap] Launch canceled!

ii、需要添加三个uses-permission:INTERNET, ACCESS_COARSE_LOCATION,ACCESS_FINE_LOCATION

iii、指示使用外部库:使用uses-library指示使用包含MapView的com.google.android.maps.jar库

      3)、GoogleMapActivity.java文件

package com.raycommtech.googlemap;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import android.os.Bundle;

public class GoogleMapActivity extends MapActivity {
	private MapView mMapView = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
                
        mMapView = (MapView)findViewById(R.id.map_view);
        mMapView.setSatellite(false);
        mMapView.setTraffic(true);
        mMapView.setBuiltInZoomControls(true);
    }

	@Override
	protected boolean isRouteDisplayed() {

		return false;
	}	
}
2、apiKey的申请

       在ubuntu上执行以下命令:

$cd ~
$keytool -list -alias androiddebugkey -keystore ~/.android/debug.keystore -storepass android -keypass android
终端上输出产生以下结果:

     androiddebugkey, Mar 3, 2011, PrivateKeyEntry,
     Certificate fingerprint (MD5): E0:D1:06:6F:BE:4D:33:1C:3F:E6:C2:9C:49:E8:2F:1A

将生成的MD5指纹输入连接里生成所需的apiKey添到MapView的android:apiKey中即可

3、程序在开发板上的部署及问题的解决

       直接运行程序,Eclipse的console直接报错如下:

[2012-02-22 13:06:32 - GoogleMap] Installation error: INSTALL_FAILED_MISSING_SHARED_LIBRARY
[2012-02-22 13:06:32 - GoogleMap] Please check logcat output for more details.
[2012-02-22 13:06:32 - GoogleMap] Launch canceled!
通过adb logcat看详细错误如下;

D/PackageParser( 1068): Scanning package: /data/app/vmdl583239231.tmp
D/PackageManager( 1068): Scanning package com.raycommtech.googlemap
E/PackageManager( 1068): Package com.raycommtech.googlemap requires unavailable shared library com.google.android.maps; failing!
W/PackageManager( 1068): Package couldn't be installed in /data/app/com.raycommtech.googlemap-1.apk
错误提示很明显,板子上的系统中没有com.google.android.maps.jar这个共享库。到Android的SDK的add-onsq/addon-google_apis-google_inc_-14/libs/目录下找到maps.jar,并将其重新命令为com.google.android.maps.jar,通过以下命令push到系统中:

$adb remount
$adb push com.google.android.maps.jar /system/framework/
重新启动系统,并执行程序,还是报上面的错误,明显添加的库没起作用,google上查了下,发现需要写一个com.google.android.maps.xml的permissions文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<permissions>
    <library name="com.google.android.maps"
             file="/system/framework/com.google.android.maps.jar" />
</permissions>

执行以下命令,将com.google.android.maps.xml文件push到系统中:

$adb remount
$adb push com.google.android.maps.jar /system/etc/permissions/
重新启动系统,并执行程序,报以下错误:

E/AndroidRuntime( 1340): FATAL EXCEPTION: main
E/AndroidRuntime( 1340): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.raycommtech.googlemap/com.raycommtech.googlemap.GoogleMapActivity}: java.lang.ClassNotFoundException: com.raycommtech.googlemap.GoogleMapActivity in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.raycommtech.googlemap-2.apk]
E/AndroidRuntime( 1340): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
E/AndroidRuntime( 1340): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime( 1340): 	at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 1340): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime( 1340): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1340): 	at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 1340): 	at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 1340): 	at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1340): 	at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 1340): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 1340): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 1340): 	at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1340): Caused by: java.lang.ClassNotFoundException: com.raycommtech.googlemap.GoogleMapActivity in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.raycommtech.googlemap-2.apk]
E/AndroidRuntime( 1340): 	at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
E/AndroidRuntime( 1340): 	at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
E/AndroidRuntime( 1340): 	at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
E/AndroidRuntime( 1340): 	at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E/AndroidRuntime( 1340): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
E/AndroidRuntime( 1340): 	... 11 more
W/ActivityManager( 1065):   Force finishing activity com.raycommtech.googlemap/.GoogleMapActivity

很明显,Android SDK中的maps不给力啊,木有所需要的MapActivity。于是只能到网上去搜索,还真下到所需要的文件,下载文件,并按照上面的步骤将com.google.android.maps.jar和com.google.android.maps.xml分别push到系统中,再次重启开发板,执行程序就OK啦,显示效果如下:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何雷 — 智能汽车

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值