最近在学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>
使用DDMS 可以向模拟器中发送经纬度,观察变化

结果

更多相关文章

  1. 【错误记录】Android 内存泄漏 错误排查记录 ( FinalizerReferen
  2. Android Studio 快速定位错误行
  3. android常见错误之 No resource found that matches the given n
  4. Android listView典型错误分析(经验体会)
  5. Android用TabLayout实现类似网易选项卡动态滑动效果
  6. android 使用Tabhost 发生could not create tab content because

随机推荐

  1. ANDROID 中UID与PID的作用与区别
  2. 使用百度siteapp开发网站的App-(IOS和And
  3. Android的按键映射
  4. Android实现深度链接(APP外带动态参数唤醒
  5. 尼尔森:Android美国市场份额39%,苹果则是最
  6. Android(安卓)API Guides---Layouts
  7. Android(安卓)获取wifi的加密方式
  8. Android(安卓)中 ListView 分页加载数据
  9. Android(安卓)避免内存泄漏(译)
  10. 如何使用Android中的OpenGL ES媒体效果