Androidが現在位置を取得する3つの方法とその使用方法

9698 ワード

1.GPS測位2.基地局がこのような位置を位置決めする取得は、携帯電話の無線通信信号に依存し、携帯電話が信号のカバー範囲内にある場合、携帯電話機は、この地域(すなわち、通信用語の「団地」)の識別番号を取得することができる.これらの識別番号は唯一であるため、識別番号と地理座標を対応させることができるので、識別番号によって地理的位置を知ることができる.しかし誤差は比較的大きい.MCC(Mobile Country Code)、MNC(Mobile Network Code)、LAC(Location Aera Code)、CID(Cell Tower ID)は通信業界の名詞である.MCCは国を識別し、MNCはネットワークを識別し、両者を組み合わせると通信事業者を唯一識別する.ウィキペディアによると、1つの国のMCCは唯一ではなく、例えば中国に460と461があり、1つの事業者も1つのMNCだけではなく、例えば中国移動には00、02、07がある.LACは地域を識別し、行政区域、事業者に類似している大きな領域をいくつかの小さな領域に分割し、各領域にLACを割り当てます.CIDは基地局を識別し、携帯電話が稼働している場合は、1つの通信基地局と通信しなければならず、CIDを通じて携帯電話の地理的範囲を特定することができる.Androidでは、通信ネットワークに関する情報の大部分は、TelephoneManagerというシステムサービスを通じて取得する必要があります.

TelephonyManager mTelMan = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String operator = mTelMan.getNetworkOperator();
String mcc = operator.substring(0, 3);
String mnc = operator.substring(3);
GsmCellLocation location = (GsmCellLocation) mTelMan.getCellLocation();
int cid = location.getCid();
int lac = location.getLac();
上記の方法により、MCC、MNC、CID、LACを得ることができ、Geolocation API Network Protocolと照合し、残りの少ないパラメータも得ることができ、要求を開始した後、応答内容に基づいて地理的位置情報を得ることができる.リクエスト(Request)の情報は次のとおりです.

{"cell_towers":[{"mobile_network_code":"00","location_area_code":9733,
"mobile_country_code":"460","cell_id":17267},{"mobile_network_code":"00","location_area_code":9733,
"mobile_country_code":"460","cell_id":27852},{"mobile_network_code":"00","location_area_code":9733,
"mobile_country_code":"460","cell_id":27215},{"mobile_network_code":"00","location_area_code":9733,
"mobile_country_code":"460","cell_id":27198},{"mobile_network_code":"00","location_area_code":9484,
"mobile_country_code":"460","cell_id":27869},{"mobile_network_code":"00","location_area_code":9508,
"mobile_country_code":"460","cell_id":37297},{"mobile_network_code":"00","location_area_code":9733,
"mobile_country_code":"460","cell_id":27888}],
"host":"maps.google.com",
"version":"1.1.0"}
応答(Response)の情報は以下の通りである.

{"location":{"latitude":23.12488,"longitude":113.271907,"accuracy":630.0},"access_token":"2:61tEAW-rONCT1_W-:JVpp2_jq5a0L-5JK"}
3.WIFI測位その原理は、まず各WIFI無線アクセスポイントの位置を収集し、各無線ルータに対して一意の識別を行い、データベースにこれらのアクセスポイントの具体的な位置を明記することである.使用時、WI-FIアクセスポイントが発見されると、データに入って一致するレコードを表示し、位置情報を得る.WIFIの位置付けは主にノード(node)の物理アドレス(mac address)に依存する.TelephoneManagerの提供と同様にAndroidはWIFI情報を取得するインタフェース:WifiManagerも提供する.

WifiManager wifiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiMan.getConnectionInfo();
String mac = info.getMacAddress();
String ssid = info.getSSID();
は、上記の方法により、必要な要求パラメータを得ることができる.リクエストメッセージは次のとおりです.

{"wifi_towers":[{"mac_address":"00:23:76:AC:41:5D","ssid":"Aspire-NETGEAR"}],"host":"maps.google.com","version":"1.1.0"}
応答(Response)の情報は以下の通りである.

{"location":{"latitude":23.129075,"longitude":113.264423,"accuracy":140000.0},"access_token":"2:WRr36ynOz_d9mbw5:pRErDAmJXI8l76MU"}
以上でandroidで位置を取得する3つの位置決め方式を簡単に紹介した.実際の開発ではandroidを利用できる.地理情報のインタフェースをカプセル化しGPS_PROVIDERとNETWORK_PROVIDER. 開発されたアプリケーションに高い精度が必要な場合はGPS_PROVIDERですが、これはアプリケーションが室内で使用できないこと、待機時間が短縮され、応答時間がやや長いことなどの問題を意味します.開発されたアプリケーションに迅速な反応が必要で、精度に対する要求があまり高くなく、できるだけ電力を節約するにはNETWORK_を使用します.PROVIDERはいい選択です.ここではPASSIVEもありますPROVIDERは、実際のアプリケーションではあまり使われていません.1.次のコードは、Networkから取得する場所を設定します.

LocationManager mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

mLocMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, mLocLis);
必要な権限:android.permission.ACCESS_COARSE_LOCATION 2.次のコードは、GPSから取得する位置を設定します.

LocationManager mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

mLocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, mLocLis);
必要な権限:android.permission.ACCESS_FINE_LOCATIONコードに2つのPROVIDERが使用されている場合は、1つの権限しか必要ありません:android.permission.ACCESS_FINE_LOCATION. 以下は、プロセス全体のコードです.

public class DemoActivity extends Activity {

private static final String TAG = "DemoActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

public void onRequestLocation(View view) {
switch (view.getId()){
case R.id.gpsBtn:
Log.d(TAG, "GPS button is clicked");
requestGPSLocation();
break;
case R.id.telBtn:
Log.d(TAG, "CellID button is clicked");
requestTelLocation();
break;
case R.id.wifiBtn:
Log.d(TAG, "WI-FI button is clicked");
requestWIFILocation();
break;
case R.id.netBtn:
Log.d(TAG, "Network button is clicked");
requestNetworkLocation();
break;
}
}

private void requestTelLocation() {
TelephonyManager mTelMan = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// MCC+MNC. Unreliable on CDMA networks
String operator = mTelMan.getNetworkOperator();
String mcc = operator.substring(0, 3);
String mnc = operator.substring(3);

GsmCellLocation location = (GsmCellLocation) mTelMan.getCellLocation();
int cid = location.getCid();
int lac = location.getLac();

JSONObject tower = new JSONObject();
try {
tower.put("cell_id", cid);
tower.put("location_area_code", lac);
tower.put("mobile_country_code", mcc);
tower.put("mobile_network_code", mnc);
} catch (JSONException e) {
Log.e(TAG, "call JSONObject's put failed", e);
}

JSONArray array = new JSONArray();
array.put(tower);

List<NeighboringCellInfo> list = mTelMan.getNeighboringCellInfo();
Iterator<NeighboringCellInfo> iter = list.iterator();
NeighboringCellInfo cellInfo;
JSONObject tempTower;
while (iter.hasNext()) {
cellInfo = iter.next();
tempTower = new JSONObject();
try {
tempTower.put("cell_id", cellInfo.getCid());
tempTower.put("location_area_code", cellInfo.getLac());
tempTower.put("mobile_country_code", mcc);
tempTower.put("mobile_network_code", mnc);
} catch (JSONException e) {
Log.e(TAG, "call JSONObject's put failed", e);
}
array.put(tempTower);
}

JSONObject object = createJSONObject("cell_towers", array);
requestLocation(object);
}

private void requestWIFILocation() {
WifiManager wifiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiMan.getConnectionInfo();
String mac = info.getMacAddress();
String ssid = info.getSSID();

JSONObject wifi = new JSONObject();
try {
wifi.put("mac_address", mac);
wifi.put("ssid", ssid);
} catch (JSONException e) {
e.printStackTrace();
}

JSONArray array = new JSONArray();
array.put(wifi);

JSONObject object = createJSONObject("wifi_towers", array);
requestLocation(object);
}

private void requestLocation(JSONObject object) {
Log.d(TAG, "requestLocation: " + object.toString());
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.google.com/loc/json");
try {
StringEntity entity = new StringEntity(object.toString());
post.setEntity(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

try {
HttpResponse resp = client.execute(post);
HttpEntity entity = resp.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuffer buffer = new StringBuffer();
String result = br.readLine();
while (result != null) {
buffer.append(result);
result = br.readLine();
}

Log.d(TAG, buffer.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private JSONObject createJSONObject(String arrayName, JSONArray array) {
JSONObject object = new JSONObject();
try {
object.put("version", "1.1.0");
object.put("host", "maps.google.com");
object.put(arrayName, array);
} catch (JSONException e) {
Log.e(TAG, "call JSONObject's put failed", e);
}
return object;
}

private void requestGPSLocation() {
LocationManager mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 60, 100, mLocLis);
}

private void requestNetworkLocation() {
LocationManager mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 60, 100, mLocLis);
}

private LocationListener mLocLis = new LocationListener() {

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, "onStatusChanged, provider = " + provider);
}

@Override
public void onProviderEnabled(String provider) {
Log.d(TAG, "onProviderEnabled, provider = " + provider);
}

@Override
public void onProviderDisabled(String provider) {
Log.d(TAG, "onProviderDisabled, provider = " + provider);
}

@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Log.d(TAG, "latitude: " + latitude + ", longitude: " + longitude);
}
};
}
この記事はインターネットに由来し、まだ実践テストを行っていません.実際のアプリケーションの関係で、ここではNetWork方式の取得位置のみを実践しています.AndroidはNetWork方式で現在の地理的位置を取得してください.関連内容を確認してください.