android获取位置数据

代码:

package com.tc.gps;

import java.util.List;

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.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private LocationManager lm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        updateView(location);
        //每3秒更新一下
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 8, new LocationListener() {

            @Override//当GPS状态改变时监听
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }

            @Override//GPS可用时监听
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override//GPS不可用时监听
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override//位置改变时监听
            public void onLocationChanged(Location location) {
                updateView(location);
            }


        });



    }
    private void updateView(Location location) {
        double lng=location.getLongitude();//经度
        double lat=location.getLatitude();//纬度
        if (location!=null) {
            StringBuilder sb=new StringBuilder();
            sb.append("实时的位置信息:\n")
            .append("经度:")
            .append(location.getLongitude())
            .append("纬度:")
            .append(location.getLatitude())
            .append("高度")
            .append(location.getAltitude())
            .append("速度")
            .append(location.getSpeed())
            .append("方向")
            .append(location.getBearing());
            Toast.makeText(this, ""+sb.toString(), Toast.LENGTH_SHORT).show();
        }
    }

}

经纬度可集成高德地图

posted @ 2016-09-21 13:46  iszengmh  阅读(111)  评论(0编辑  收藏  举报