下了“深入浅出google android”这本书中的有关googlemap相关的几个demo源码,其中一个最简单的是获取当前经纬度,
就一个类,
public class CurrentLocation extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(serviceName);
//String provider = LocationManager.GPS_PROVIDER;
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,
Bundle extras){ }
};
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "纬度:" + lat + "\n经度:" + lng;
} else {
latLongString = "无法获取地理信息";
}
myLocationText.setText("您当前的位置是:\n" +
latLongString);
}
}
我直接倒入到eclipse里,然后通过真机测试,竟然提示“无法获取地理信息”(G5,设置正常),模拟器测试也是“无法获取地理信息”,不知道怎么回事,请高手指点指点(相关源码)
就一个类,
public class CurrentLocation extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(serviceName);
//String provider = LocationManager.GPS_PROVIDER;
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,
Bundle extras){ }
};
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "纬度:" + lat + "\n经度:" + lng;
} else {
latLongString = "无法获取地理信息";
}
myLocationText.setText("您当前的位置是:\n" +
latLongString);
}
}
我直接倒入到eclipse里,然后通过真机测试,竟然提示“无法获取地理信息”(G5,设置正常),模拟器测试也是“无法获取地理信息”,不知道怎么回事,请高手指点指点(相关源码)
解决方案 »
- 请教setFlags方法
- 怎样实现pageview左右滑动时 上面的标题也跟着动啊
- Android Map API Key申请问题
- 广告平台哪个好?哪个收入高?
- 急!有关使用EXPANABLEDLISTVIEW及多线程下载暂停继续的问题
- 平板电脑里面怎么实现一个表格表头不动下边上下滚动的
- 纠结一周了!我写的一个桌面应用的程序,老是提示崩溃是什么原因呢?
- android 应用速度缓慢(grow heap frag case 12MB...)
- android6.0的Launcher3怎么修改在桌面显示所有APP,怎么设置默认Hotseat?
- 乌班图下编译ffmpeg,其余的都ok 就是运行.sh后输入make出现了一个c的错误
- Help g711用java写的编解码例子
- android中实现百度地图,在线等
11-12 04:34:30.692: DEBUG/ResourceType(732): calling getConfigurations
11-12 04:34:30.735: DEBUG/ResourceType(732): called getConfigurations size=118
11-12 04:34:30.737: DEBUG/asset(732): locale 0: ''
11-12 04:34:30.747: DEBUG/asset(732): locale 1: 'ja'
11-12 04:34:30.757: DEBUG/asset(732): locale 2: 'de'
11-12 04:34:30.767: DEBUG/asset(732): locale 3: 'nl'
11-12 04:34:30.767: DEBUG/asset(732): locale 4: 'pl'
11-12 04:34:30.777: DEBUG/asset(732): locale 5: 'ko'
11-12 04:34:30.798: DEBUG/asset(732): locale 6: 'fr'
11-12 04:34:30.798: DEBUG/asset(732): locale 7: 'cs'
11-12 04:34:30.798: DEBUG/asset(732): locale 8: 'es'
11-12 04:34:30.798: DEBUG/asset(732): locale 9: 'it'
11-12 04:34:30.798: DEBUG/asset(732): locale 10: 'ru'
11-12 04:34:30.798: DEBUG/asset(732): locale 11: 'en_GB'
11-12 04:34:30.798: DEBUG/asset(732): locale 12: 'en_SG'
11-12 04:34:30.798: DEBUG/asset(732): locale 13: 'zh_CN'
11-12 04:34:30.798: DEBUG/asset(732): locale 14: 'en_US'
11-12 04:34:30.798: DEBUG/asset(732): locale 15: 'en_AU'
11-12 04:34:30.798: DEBUG/asset(732): locale 16: 'zh_TW'
11-12 04:34:30.798: DEBUG/asset(732): locale 17: 'en_CA'
11-12 04:34:30.798: DEBUG/asset(732): locale 18: 'fr_CA'
11-12 04:34:30.798: DEBUG/asset(732): locale 19: 'nl_BE'
11-12 04:34:30.798: DEBUG/asset(732): locale 20: 'fr_BE'
11-12 04:34:30.798: DEBUG/asset(732): locale 21: 'de_DE'
11-12 04:34:30.798: DEBUG/asset(732): locale 22: 'de_CH'
11-12 04:34:30.798: DEBUG/asset(732): locale 23: 'fr_CH'
11-12 04:34:30.798: DEBUG/asset(732): locale 24: 'it_CH'
11-12 04:34:30.798: DEBUG/asset(732): locale 25: 'de_LI'
11-12 04:34:30.798: DEBUG/asset(732): locale 26: 'nl_NL'
11-12 04:34:30.798: DEBUG/asset(732): locale 27: 'pl_PL'
11-12 04:34:30.798: DEBUG/asset(732): locale 28: 'ja_JP'
11-12 04:34:30.798: DEBUG/asset(732): locale 29: 'fr_FR'
11-12 04:34:30.798: DEBUG/asset(732): locale 30: 'ko_KR'
11-12 04:34:30.817: DEBUG/asset(732): locale 31: 'es_ES'
11-12 04:34:30.817: DEBUG/asset(732): locale 32: 'de_AT'
11-12 04:34:30.817: DEBUG/asset(732): locale 33: 'it_IT'
11-12 04:34:30.817: DEBUG/asset(732): locale 34: 'ru_RU'
11-12 04:34:30.817: DEBUG/asset(732): locale 35: 'cs_CZ'
11-12 04:34:30.817: DEBUG/asset(732): locale 36: 'en_NZ'
11-12 04:34:30.817: DEBUG/asset(732): locale 37: 'en'
11-12 04:34:30.978: DEBUG/LocaleSetup(732): customLocales:
11-12 04:34:31.487: WARN/KeyCharacterMap(732): No keyboard for id 0
11-12 04:34:31.607: WARN/KeyCharacterMap(732): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-12 04:34:31.801: WARN/InputManagerService(571): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@437247b0 (uid=10004 pid=732)
11-12 04:34:31.817: WARN/InputManagerService(571): Client not active, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@436d3418
11-12 04:34:32.148: WARN/ActivityManager(571): Activity pause timeout for HistoryRecord{436bd608 {com.android.customlocale/com.android.customlocale.CustomLocaleActivity}}
11-12 04:34:32.367: WARN/InputManagerService(571): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4371eb60
11-12 04:34:33.797: DEBUG/dalvikvm(622): GC freed 1879 objects / 97256 bytes in 184ms
11-12 04:34:36.517: INFO/ActivityManager(571): Starting activity: Intent { action=android.intent.action.MAIN categories={android.intent.category.LAUNCHER} flags=0x10200000 comp={com.studio.android.chp08.ex01/com.studio.android.chp08.ex01.CurrentLocation} }
11-12 04:34:37.237: DEBUG/GpsLocationProvider(571): setMinTime 2000
11-12 04:34:37.577: WARN/IInputConnectionWrapper(622): showStatusIcon on inactive InputConnection
11-12 04:34:37.807: INFO/ActivityManager(571): Displayed activity com.studio.android.chp08.ex01/.CurrentLocation: 1241 ms
11-12 04:34:42.958: DEBUG/dalvikvm(622): GC freed 147 objects / 6264 bytes in 122ms
打log:
11-12 04:39:31.838: DEBUG/dalvikvm(622): GC freed 227 objects / 10272 bytes in 155ms
11-12 04:39:41.287: WARN/KeyCharacterMap(864): No keyboard for id 0
11-12 04:39:41.307: WARN/KeyCharacterMap(864): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-12 04:39:44.527: WARN/KeyCharacterMap(622): No keyboard for id 0
11-12 04:39:44.538: WARN/KeyCharacterMap(622): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-12 04:39:46.287: INFO/ActivityManager(571): Starting activity: Intent { action=android.intent.action.MAIN categories={android.intent.category.LAUNCHER} flags=0x10200000 comp={com.studio.android.chp08.ex01/com.studio.android.chp08.ex01.CurrentLocation} }
11-12 04:39:46.798: DEBUG/location(864): locationnull
11-12 04:39:46.818: DEBUG/GpsLocationProvider(571): setMinTime 2000
11-12 04:39:47.247: WARN/IInputConnectionWrapper(622): showStatusIcon on inactive InputConnection
11-12 04:39:47.437: INFO/ActivityManager(571): Displayed activity com.studio.android.chp08.ex01/.CurrentLocation: 1120 ms
11-12 04:39:52.638: DEBUG/dalvikvm(622): GC freed 155 objects / 6952 bytes in 170ms
好像检测不到信息
不知道上面这东西有帮助没
2、真机中不能设置为飞行模式,不然GPS没用。
3、尽量在室处,卫星需要一些时间。
4、检查代码。。
5、对于模拟器中可以用DDMS模拟经纬度值唉,这个都搞烂了,大体就这么多问题。
你能把此例子发一份给我email吗 [email protected] thanks