代码如下:
package com.gpstest;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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;public class GpsTest extends Activity {
OnClickListener listener1 = null;
LocationListener locationlistener =null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationUpdate(locationManager);
//按钮接受事件
final Button gpsButton = (Button)findViewById(R.id.butt);
gpsButton.setOnClickListener(listener1);
listener1 = new OnClickListener(){ @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
LoadCoords(locationManager);
}
};
} private void LocationUpdate(LocationManager locationManager) {
// TODO Auto-generated method stub
locationlistener= new LocationListener(){ @Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location != null){
Log.e("SupperMap", "Location changed : Lat:"+location.getLatitude()+"Lng:"+location.getLongitude());
}else{
Log.e("faild", "没有卫星地址!!"); }
} @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
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationlistener);
}
private void LoadCoords(LocationManager locationManager) {
// TODO Auto-generated method stub
TextView latText = (TextView) findViewById(R.id.Latitude);
TextView lngText = (TextView) findViewById(R.id.Langitude);
Location location = locationManager.getLastKnownLocation("gps");
if(null == location){
setTitle("定位信息失败");
}else{
latText.setText(Double.toString(location.getLatitude()));
lngText.setText(Double.toString(location.getLongitude()));
}
}
}可是却获取不到一直都是“定位信息失败”。,到底是哪里出问题,
后来我看了下,好像又要需要导入什么KML文件什么的。大家有没知道的?还是怎么解决?
package com.gpstest;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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;public class GpsTest extends Activity {
OnClickListener listener1 = null;
LocationListener locationlistener =null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationUpdate(locationManager);
//按钮接受事件
final Button gpsButton = (Button)findViewById(R.id.butt);
gpsButton.setOnClickListener(listener1);
listener1 = new OnClickListener(){ @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
LoadCoords(locationManager);
}
};
} private void LocationUpdate(LocationManager locationManager) {
// TODO Auto-generated method stub
locationlistener= new LocationListener(){ @Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location != null){
Log.e("SupperMap", "Location changed : Lat:"+location.getLatitude()+"Lng:"+location.getLongitude());
}else{
Log.e("faild", "没有卫星地址!!"); }
} @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
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationlistener);
}
private void LoadCoords(LocationManager locationManager) {
// TODO Auto-generated method stub
TextView latText = (TextView) findViewById(R.id.Latitude);
TextView lngText = (TextView) findViewById(R.id.Langitude);
Location location = locationManager.getLastKnownLocation("gps");
if(null == location){
setTitle("定位信息失败");
}else{
latText.setText(Double.toString(location.getLatitude()));
lngText.setText(Double.toString(location.getLongitude()));
}
}
}可是却获取不到一直都是“定位信息失败”。,到底是哪里出问题,
后来我看了下,好像又要需要导入什么KML文件什么的。大家有没知道的?还是怎么解决?
解决方案 »
- 装个Adroid-sdk怎么这么费劲!一下午一晚上了没弄完!
- 关于notification 的问题--怎么保留activity的状态
- 自定义Adapter后数据显示问题
- 错误
- 腾讯QQ listView Scrollbar 透明
- 冰天雪地五花大绑裸跪求ANDROID难题解答。
- ListView滚动的问题
- intent传递list:PackageInfo.activities,PackageInfo.services,PackageInfo.permissions
- svg中图形元素的点击事件,求教!!!!!!!
- Activity取消切换动画效果
- 求助,为什么我做了跟这个一样的列子,信息硬是报找不到我的javabean呢?
- 自己编译的android内核如何烧录到手机上?
打上勾
如果是真机,则不能在室内测试,一定要在室外才能测试,当然你要将GPS功能开启才行
对,记得模拟器通过QEMUD这个DEAMON进程来中转数据的.