最近在学Android,想做个google-map-api的小应用,哪知道费死劲了……
首先是关于<uses-library />的引用位置,书中没有提及,结果程序一运行就出现has stopped unexpectedly错误,我一开始以为api给的key有问题,于是重新申请了一下,可是问题仍在,于是网上一顿狂搜,发现应该是这样滴:
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Main"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <uses-library android:name="com.google.android.maps" />
    </application>
注意,这个<uses-library />标签只在<application />标签中才能添加,位置放不对,作为第三方库,就不会被正确加载。

 
改完这里,继续运行,仍然出现错误,还是has stopped unexpectedly,最后几经实验,终于把问题锁定在了MapActivity这个类上,因为在如同往常一样的继承Activity类的时候,一切正常,GPS也能模拟使用,但是如果一旦继承了MapActivity就会出现上述错误了。突然想起了是不是在build的过程中,库的问题,然后把属性中,Android选项的Project build target选项从Android1.5改选为Google APIs类型,这时候,还得把按照书中作为第三方库添加进入JAVA build path的map.jar库删掉,因为选择了这个之后,在Google APIs里面自动带了mar.jar库。
 
这里其实还会连接网络,所以一般会添加两个权限,在application后面
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
我自己犯了个很傻B的错误,把library拼成libaray,然后找了一天,才发现。
 
建了个Main类   可以即时获取经纬度的变化信息,并显示出来
package com.gavin.googleMap;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class Main extends Activity {
 private static final String TAG = "Main";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        String serviceString = Context.LOCATION_SERVICE;
        LocationManager locationManager = (LocationManager)getSystemService(serviceString);
       
        String provider = LocationManager.GPS_PROVIDER;
        Location location = locationManager.getLastKnownLocation(provider);
        getLocationInfo(location);
        locationManager.requestLocationUpdates(provider,2000,0,locationListener);
    }
   
    private void getLocationInfo(Location location){
     String locationInfo;
     TextView locationText = (TextView)findViewById(R.id.locationInfoText);
     if(location != null){
      double latitude = location.getLatitude();   //获取经纬度
      double longitude = location.getLongitude();
      locationInfo = "Latitude:" + latitude + "\nLongitude:" + longitude;
     }else {
      locationInfo = "No location found";
     }
     locationText.setText("Your currentPosition is :\n" + locationInfo);
    }
   
    private final LocationListener locationListener = new LocationListener(){
  @Override
  public void onLocationChanged(Location location) {
   // TODO Auto-generated method stub
   getLocationInfo(location);
  }
  @Override
  public void onProviderDisabled(String arg0) {
   // TODO Auto-generated method stub
  }
  @Override
  public void onProviderEnabled(String arg0) {
   // TODO Auto-generated method stub
  }
  @Override
  public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
   // TODO Auto-generated method stub
  }
    };
}
//main.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:id="@+id/locationInfoText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>
 
Android google Map 出现问题
 
使用DDMS 可以向模拟器中发送经纬度,观察变化
 

Android google Map 出现问题 

结果

 

Android google Map 出现问题