android 使用gps获取经纬度

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
<Button android:id="@+id/btn"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="获取当前经纬度"/>

<TextView android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/test"/>
    

</LinearLayout>



package com.clickmapview.main;


import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class Test extends Activity {
    /** Called when the activity is first created. */
Button button;
TextView textview;
LocationManager manager;
Location location;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        textview=(TextView)findViewById(R.id.test);
        button=(Button)findViewById(R.id.btn);
        manager=(LocationManager)getSystemService(LOCATION_SERVICE);
        //从GPS_PROVIDER获取最近的定位信息
        location=manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        updateView(location);
        //判断GPS是否可用
        System.out.println("state="+manager.isProviderEnabled(LocationManager.GPS_PROVIDER));
        button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//设置每5秒,每移动5米向LocationProvider获取一次GPS的定位信息
//当LocationProvider可用,不可用或定位信息改变时,调用updateView,更新显示
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, new LocationListener() {

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
//
updateView(manager.getLastKnownLocation(provider));
}

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

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
//location为变化完的新位置,更新显示
updateView(location);
}
});
}
});
    }
    //更新显示内容的方法
    public void updateView(Location location)
    {
    StringBuffer buffer=new StringBuffer();
    if(location==null)
    {
    textview.setText("未获得服务");
    return;
    }
    buffer.append("经度:"+location.getLongitude()+"\n");
    buffer.append("纬度:"+location.getLatitude()+"\n");
    buffer.append("高度:"+location.getAltitude()+"\n");
    buffer.append("速度:"+location.getSpeed()+"\n");
    buffer.append("方向:"+location.getBearing()+"\n");
    textview.setText(buffer.toString());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值