您的位置:首页 > 其它

GPS获取当前的经度和纬度

2012-06-02 13:47 183 查看
package com.cn.gps;

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 GpsActivity extends Activity {

/** Called when the activity is first created. */

private TextView text;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

final LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

text = (TextView)findViewById(R.id.tv1);

Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

showLocation(location);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, new LocationListener(){

public void onLocationChanged(Location location) {

// TODO Auto-generated method stub

showLocation(location);

}

public void onProviderDisabled(String provider) {

// TODO Auto-generated method stub

showLocation(null);

}

public void onProviderEnabled(String provider) {

// TODO Auto-generated method stub

showLocation(locationManager.getLastKnownLocation(provider));

}

public void onStatusChanged(String provider, int status, Bundle extras) {

// TODO Auto-generated method stub

}

});

}

public void showLocation(Location currentLocation){

if(currentLocation != null){

String s = "";

s += "当前位置: (";

s += currentLocation.getLongitude();

s += ",";

s += currentLocation.getLatitude();

s += ")\n 速度: ";

s += currentLocation.getSpeed();

s += "\n 方向: ";

s += currentLocation.getBearing();

s +="\n 高度:";

s += currentLocation.getAltitude();

text.setText(s);

}

else{

text.setText("");

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: