2015/9/10

2015/9/10问题集

Android取得经纬度

从中取得经纬度。
记得要添加权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

-

package com.zsz.develop.gpsfixdemo;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class GPSActivity extends AppCompatActivity {
String latLongString;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gps);
    init();
}

private void init() {
    /**
     *取得系统位置管理的引用,
     *getSystem这个一定要在activity里面引用
     */

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    //添加位置监听。Define a listener that responds to location updates
    LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the network location provider.
            //当找到新的位置提供者
            makeUseOfNewLocation(location);
        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };
    // 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}

private void makeUseOfNewLocation(Location location) {
    TextView tv1;
    tv1 = (TextView) this.findViewById(R.id.tv1);
    if (location != null) {
        double  latitude = location.getLatitude();
        double longitude= location.getLongitude();
        tv1.setText("维度:" +  latitude+ "\n经度" + longitude);
    } else {
        tv1.setText("无法获取地理信息");
    }
}

如果还要将经纬度转化为城市

package com.zsz.develop.gpsfixdemo;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class GPSActivity extends AppCompatActivity {
String latLongString;
double  latitude;
double longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gps);
    init();
}

private void init() {

    openGPSSettings();
    getLaction();

}
private void openGPSSettings() {
    LocationManager alm = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);
    if (alm
            .isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
        Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT)
                .show();
        Log.w("gps ", "正常");
        return;
    }

    Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
    startActivityForResult(intent, 0); //此为设置完成后返回到获取界面

}
private void getLaction(){
    /**
     *取得系统位置管理的引用,
     *getSystem这个一定要在activity里面引用
     */

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

    String serviceName = Context.LOCATION_SERVICE;
    locationManager = (LocationManager) this.getSystemService(serviceName);
    // 查找到服务信息
    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 = locationManager.getBestProvider(criteria, true); // 获取GPS信息

    //添加位置监听。Define a listener that responds to location updates
    LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the network location provider.
            //当找到新的位置提供者
            makeUseOfNewLocation(location);
        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };
    // 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米
    locationManager.requestLocationUpdates(provider, 1000 * 60, 100, locationListener);

}

private void makeUseOfNewLocation(Location location) {
    TextView tv1;

    tv1 = (TextView) this.findViewById(R.id.tv1);
    if (location != null) {
         latitude = location.getLatitude();
        longitude= location.getLongitude();
        tv1.setText("维度:" +  latitude+ "\n经度" + longitude);
    } else {
        tv1.setText("无法获取地理信息");
    }



    List<Address> addList = null;
    Geocoder ge = new Geocoder(this);
    try {
        addList = ge.getFromLocation(latitude, longitude, 2);
        tv1.append(addList+"");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if(addList!=null && addList.size()>0){
        for(int i=0; i<addList.size(); i++){
            Address ad = addList.get(i);
            latLongString += "\n";
            latLongString += ad.getCountryName() + ";" + ad.getLocality();
            tv1.append("您当前的位置是:\n" + latLongString);
        }
    }

}



}

Location服务之Geocoder

提到Android基于位置的服务,就不得不提android.location包,

location包提供了很便捷的API来实现基于位置的服务。主要包括Geocoder和LocationManager。今天就先来介绍一下Geocoder。

Geocoder可以在街道地址和经纬度地图坐标之间进行转换。它提供了对两种地理编码功能的访问:

Forward Geocoding(前向地理编码):查找某个地址的经纬度

Reverse Geocoding(反向地理编码):查找一个给定的经纬度所对应的街道地址。

Location获取地理位置信息(中)Criteria类的简单使用

在使用android lcoation的时候,可能不希望自己去硬性的选择是GPS服务还是NETWORK服务,可能是我们考虑的因素有很多,自己很难决定,Android SDK提供了一个类Criteria,直译为标准。

//翻译:返回满足给定的criteria(标准)的最佳provider,(前面一篇文章提到过两种重要provider的区别)。只有当provider权限允许的时候才会返回,如果几个provider均满足criteria设定的标准,最贴近标准的provider将会被返回。如果没有provider满足,标准将按照下面的序列放宽松。