android 原生GPS定位

package com.hzg.my_record;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by Jay on 2015/8/28 0028.
 */
public class MyFragment4 extends Fragment {

    private Button btnStart;
    private Button btnStop;
    private TextView textView;
    private Location mLocation;
    private LocationManager mLocationManager;


    public MyFragment4() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_content,container,false);

        btnStart = (Button)view.findViewById(R.id.btnStart);
        btnStop = (Button)view.findViewById(R.id.btnStop);
        textView = (TextView)view.findViewById(R.id.txt_content);
        btnStart.setOnClickListener(btnClickListener); //開始定位
        btnStop.setOnClickListener(btnClickListener); //結束定位按鈕

        return view;
    }

    public Button.OnClickListener btnClickListener = new Button.OnClickListener()
    {
        public void onClick(View v)
        {
            Button btn = (Button)v;
            if(btn.getId() == R.id.btnStart)
            {
                if(!gpsIsOpen())
                    return;

                mLocation = getLocation();

                if(mLocation != null)

                    textView.setText("維度:" + mLocation.getLatitude() + "\n經度:" + mLocation.getLongitude());
                else
                    textView.setText("獲取不到數據");
            }
            else if(btn.getId() == R.id.btnStop)
            {
                mLocationManager.removeUpdates(locationListener);
            }

        }
    };

    private boolean gpsIsOpen()
    {
        boolean bRet = true;

        LocationManager alm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
        if(!alm.isProviderEnabled(LocationManager.GPS_PROVIDER))
        {
            Toast.makeText(getActivity(), "未開啓GPS", Toast.LENGTH_SHORT).show();
            bRet = false;
        }
        else
        {
            Toast.makeText(getActivity(), "GPS已開啓", Toast.LENGTH_SHORT).show();
        }

        return bRet;
    }



    private Location getLocation()
    {
        //獲取位置管理服務
        mLocationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);

        //查找服務信息
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE); //定位精度: 最高
        criteria.setAltitudeRequired(false); //海拔信息:不須要
        criteria.setBearingRequired(false); //方位信息: 不須要
        criteria.setCostAllowed(true);  //是否容許付費
        criteria.setPowerRequirement(Criteria.POWER_LOW); //耗電量: 低功耗

        String provider = mLocationManager.getBestProvider(criteria, true); //獲取GPS信息

        Location location = mLocationManager.getLastKnownLocation(provider);

        mLocationManager.requestLocationUpdates(provider, 2000, 5, locationListener);

        return location;
    }

    private final LocationListener locationListener = new LocationListener()
    {
        public void onLocationChanged(Location location)
        {
            // TODO Auto-generated method stub
            if(location != null)
                textView.setText("維度:" + location.getLatitude() + "\n經度:"
                        + location.getLongitude());
            else
                textView.setText("獲取不到數據");
        }

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

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

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

        }
    };
}
相關文章
相關標籤/搜索