首页 新闻 赞助 找找看

关于android gps获取当前位置问题,希望高手帮忙

0
悬赏园豆:20 [待解决问题]

话就不多说了,直接上代码吧,希望高手帮忙

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
/** 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 latLongInfo;
TextView locationText
= (TextView)findViewById(R.id.tv1);
if (location != null){
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongInfo
= "Lat: " + lat + "\nLong: " + lng;
}
else{
latLongInfo
= "No location found";
}
locationText.setText(
"Your Current Position is:\n" + latLongInfo);
}

private final LocationListener locationListener = new LocationListener(){
@Override

public void onLocationChanged(Location location) {
getLocationInfo(location);
}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// 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:layout_width="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/hello"
android:id
="@+id/tv1"
/>
</LinearLayout>
下面是AndroidManifest.xml中的代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package
="android.bzu.hzp"
android:versionCode
="1"
android:versionName
="1.0">
<uses-sdk android:minSdkVersion="3" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<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>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
</application>
</manifest>
在虚拟机中运行时显示

孤鹜的主页 孤鹜 | 初学一级 | 园豆:180
提问于:2011-06-28 22:56
< >
分享
所有回答(2)
0

是什么问题啊?

jfttcjl | 园豆:205 (菜鸟二级) | 2011-07-25 11:25
0

其实,没有看懂楼主要问的是什么问题,那个图片显示不了。

可能会存在一些几个方面的问题吧:

1、用户权限的问题,看看是否拥有GPS相关的权限;

2、检查是否打开了GPS;

3、实验GPS一定要在室外进行,在室内是检测不到GPS信号的。

疯狂の小石子 | 园豆:195 (初学一级) | 2011-07-27 17:46
刚刷新看到图了,楼主能不能把堆栈信息发上来看看。
支持(0) 反对(0) 疯狂の小石子 | 园豆:195 (初学一级) | 2011-07-27 17:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册