展会信息港展会大全

利用Google接口实现基站定位 android开发教程
来源:互联网   发布日期:2015-09-24 20:30:43   浏览:5436次  

导读:利用Google接口实现基站定位 android开发教程1.[代码]LocationAct.java package lab.sodino.location;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import jav......

利用Google接口实现基站定位 android开发教程

1. [代码]LocationAct.java

package lab.sodino.location;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.telephony.TelephonyManager;

import android.telephony.gsm.GsmCellLocation;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

/**

* Google定位的实现.<br/>

* Geolocation的详细信息请参见:<br/>

* <a

* href="http://code.google.com/apis/gears/geolocation_network_protocol.html">

* http://code.google.com/apis/gears/geolocation_network_protocol.html</a>

*/

public class LocationAct extends Activity {

private TextView txtInfo;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button btn = (Button) findViewById(R.id.btnStart);

txtInfo = (TextView) findViewById(R.id.txtInfo);

btn.setOnClickListener(new Button.OnClickListener() {

public void onClick(View view) {

getLocation();

}

});

}

private void getLocation() {

TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

GsmCellLocation gsmCell = (GsmCellLocation) tm.getCellLocation();

int cid = gsmCell.getCid();

int lac = gsmCell.getLac();

String netOperator = tm.getNetworkOperator();

int mcc = Integer.valueOf(netOperator.substring(0, 3));

int mnc = Integer.valueOf(netOperator.substring(3, 5));

JSONObject holder = new JSONObject();

JSONArray array = new JSONArray();

JSONObject data = new JSONObject();

try {

holder.put("version", "1.1.0");

holder.put("host", "maps.google.com");

holder.put("address_language", "zh_CN");

holder.put("request_address", true);

holder.put("radio_type", "gsm");

holder.put("carrier", "HTC");

data.put("cell_id", cid);

data.put("location_area_code", lac);

data.put("mobile_countyr_code", mcc);

data.put("mobile_network_code", mnc);

array.put(data);

holder.put("cell_towers", array);

} catch (JSONException e) {

e.printStackTrace();

}

DefaultHttpClient client = new DefaultHttpClient();

HttpPost httpPost = new HttpPost("http://www.google.com/loc/json");

StringEntity stringEntity = null;

try {

stringEntity = new StringEntity(holder.toString());

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

httpPost.setEntity(stringEntity);

HttpResponse httpResponse = null;

try {

httpResponse = client.execute(httpPost);

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

HttpEntity httpEntity = httpResponse.getEntity();

InputStream is = null;

try {

is = httpEntity.getContent();

} catch (IllegalStateException e) {

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

InputStreamReader isr = new InputStreamReader(is);

BufferedReader reader = new BufferedReader(isr);

StringBuffer stringBuffer = new StringBuffer();

try {

String result = "";

while ((result = reader.readLine()) != null) {

stringBuffer.append(result);

}

} catch (IOException e) {

e.printStackTrace();

}

System.out.println("[sodino]" + stringBuffer.toString());

txtInfo.setText(stringBuffer.toString());

}

}

2. [代码]权限设定

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

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

3. [图片] 1.jpg

赞助本站

人工智能实验室

相关热词: Google接口 基站定位

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港