android 定位坐标转换,[转]android gps和lbs定位获取经纬度的代码

该博客详细介绍了Android中不同类型的定位技术,包括GPS、CellID、WI-FI和网络定位。通过示例代码展示了如何获取和发送手机的电信信息、Wi-Fi信息到指定服务器。同时,还实现了监听GPS和网络定位更新的功能。
摘要由CSDN通过智能技术生成

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);

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 list =

mTelMan.getNeighboringCellInfo();

Iterator

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);

}

};

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值