`
104zz
  • 浏览: 1503788 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

android 基于基站,apn,gps,wifi,network 根据不同手机sim卡获取经纬度

阅读更多

 

一:新建MyLocation类,本类主要管理使用各种获取经纬度的方法,由于代码比较多就不一一解释直接上代码:

package com.android.location2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
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.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.widget.Toast;
import com.android.gps.AddressTask;
import com.android.gps.CellIDInfo;
import com.android.gps.GpsTask;
import com.android.gps.GpsTask.GpsData;
import com.android.gps.GpsTaskCallBack;
import com.android.gps.IAddressTask;
import com.android.gps.IAddressTask.MLocation;

public class MyLocation {

	private LocationCallBack callBack;
	private boolean status = false;

	public MyLocation(LocationCallBack callBack, Activity cxt) {
		this.callBack = callBack;
		if (getMobileType(cxt) == 3) {

			TelephonyManager tm = (TelephonyManager) cxt
					.getSystemService(Context.TELEPHONY_SERVICE);
			CdmaCellLocation location = (CdmaCellLocation) tm.getCellLocation();
			if (location == null)
				return;
			int sid = location.getSystemId();// 系统标识 mobileNetworkCode
			int bid = location.getBaseStationId();// 基站小区号 cellId
			int nid = location.getNetworkId();// 网络标识 locationAreaCode
			ArrayList<CellIDInfo> CellID = new ArrayList<CellIDInfo>();
			CellIDInfo info = new CellIDInfo();
			info.cellId = bid;
			info.locationAreaCode = nid;
			info.mobileNetworkCode = String.valueOf(sid);
			info.mobileCountryCode = tm.getNetworkOperator().substring(0, 3);
			info.mobileCountryCode = tm.getNetworkOperator().substring(3, 5);
			info.radioType = "cdma";
			CellID.add(info);
			Location mLocation = callGear(CellID);
			if (mLocation != null) {
				callBack.onCurrentLocation(mLocation.getLatitude(),
						mLocation.getLongitude());
			}
		} else {
			Location mLocation = getLocation(cxt);
			if (mLocation != null) {
				callBack.onCurrentLocation(mLocation.getLatitude(),
						mLocation.getLongitude());
			} else {
				getMeLocation(cxt);
			}
		}
	}

	public interface LocationCallBack {
		void onCurrentLocation(double latitude, double longitude);

	}

	// 调用google gears的方法,该方法调用gears来获取经纬度
	private Location callGear(ArrayList<CellIDInfo> cellID) {
		if (cellID == null)
			return null;

		DefaultHttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost("http://www.google.com/loc/json");
		JSONObject holder = new JSONObject();

		try {
			holder.put("version", "1.1.0");
			holder.put("host", "maps.google.com");
			holder.put("home_mobile_country_code",
					cellID.get(0).mobileCountryCode);
			holder.put("home_mobile_network_code",
					cellID.get(0).mobileNetworkCode);
			holder.put("radio_type", cellID.get(0).radioType);
			holder.put("request_address", true);
			if ("460".equals(cellID.get(0).mobileCountryCode))
				holder.put("address_language", "zh_CN");
			else
				holder.put("address_language", "en_US");

			JSONObject data, current_data;

			JSONArray array = new JSONArray();

			current_data = new JSONObject();
			current_data.put("cell_id", cellID.get(0).cellId);
			current_data.put("location_area_code",
					cellID.get(0).locationAreaCode);
			current_data.put("mobile_country_code",
					cellID.get(0).mobileCountryCode);
			current_data.put("mobile_network_code",
					cellID.get(0).mobileNetworkCode);
			current_data.put("age", 0);
			current_data.put("signal_strength", -60);
			current_data.put("timing_advance", 5555);
			array.put(current_data);

			holder.put("cell_towers", array);

			StringEntity se = new StringEntity(holder.toString());
			post.setEntity(se);
			HttpResponse resp = client.execute(post);

			HttpEntity entity = resp.getEntity();

			BufferedReader br = new BufferedReader(new InputStreamReader(
					entity.getContent()));
			StringBuffer sb = new StringBuffer();
			String result = br.readLine();
			while (result != null) {
				sb.append(result);
				result = br.readLine();
			}

			data = new JSONObject(sb.toString());
			data = (JSONObject) data.get("location");

			Location loc = new Location(LocationManager.NETWORK_PROVIDER);
			loc.setLatitude((Double) data.get("latitude"));
			loc.setLongitude((Double) data.get("longitude"));
			loc.setAccuracy(Float.parseFloat(data.get("accuracy").toString()));
			loc.setTime(System.currentTimeMillis());// AppUtil.getUTCTime());
			return loc;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public void getMeLocation(final Activity cxt) {
		do_apn(cxt);
		if (status) {
			status = false;
			do_wifi(cxt);
		} else if (status) {
			status = false;
			GpsTask gpstask = new GpsTask(cxt, new GpsTaskCallBack() {

				public void gpsConnectedTimeOut() {
					Toast.makeText(cxt, "Error", Toast.LENGTH_LONG).show();
				}

				public void gpsConnected(GpsData gpsdata) {
					do_gps(gpsdata, cxt);
				}

			}, 3000);
			gpstask.execute();
		}

	}

	private void do_apn(final Activity cxt) {

		new AsyncTask<Void, Void, MLocation>() {

			protected MLocation doInBackground(Void... params) {
				MLocation location = null;
				try {
					location = new AddressTask(cxt, IAddressTask.DO_APN)
							.doApnPost();
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (location != null)
					return location;
				else
					return null;
			}

			protected void onPreExecute() {
				super.onPreExecute();
			}

			protected void onPostExecute(MLocation result) {
				if (result != null) {
					setData(result);
					status = true;
				} else {
					status = false;
				}
				super.onPostExecute(result);
			}

		}.execute();

	}

	private void do_gps(final GpsData gpsdata, final Activity cxt) {
		new AsyncTask<Void, Void, MLocation>() {

			protected MLocation doInBackground(Void... params) {
				MLocation location = null;
				try {
					location = new AddressTask(cxt, IAddressTask.DO_GPS)
							.doGpsPost(gpsdata.getLatitude(),
									gpsdata.getLongitude());
				} catch (Exception e) {
					e.printStackTrace();
				}
				return location;
			}

			protected void onPreExecute() {
				super.onPreExecute();
			}

			protected void onPostExecute(MLocation result) {
				// gps_tip.setText(result);
				if (result != null) {
					setData(result);
					status = true;
				} else {
					status = false;
				}
				super.onPostExecute(result);
			}

		}.execute();

	}

	private void do_wifi(final Activity cxt) {
		new AsyncTask<Void, Void, MLocation>() {

			protected MLocation doInBackground(Void... params) {
				MLocation location = null;
				try {
					location = new AddressTask(cxt, IAddressTask.DO_WIFI)
							.doWifiPost();
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (location == null) {
					return null;
				} else
					return location;
			}

			protected void onPreExecute() {
				super.onPreExecute();
			}

			protected void onPostExecute(MLocation result) {
				if (result != null) {
					setData(result);
					status = true;
				} else {
					status = false;
				}
				super.onPostExecute(result);
			}

		}.execute();

	}

	private void setData(MLocation result) {

		callBack.onCurrentLocation(result.getLatitude(), result.getLongitude());
	}

	// Get the Location by GPS or WIFI
	public Location getLocation(Context context) {
		LocationManager locMan = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		Location location = locMan
				.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		if (location == null) {
			location = locMan
					.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		}
		return location;
	}

	private int getMobileType(Context context) {
		TelephonyManager iPhoneManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String iNumeric = iPhoneManager.getSimOperator();
		if (iNumeric.length() > 0) {
			if (iNumeric.equals("46000") || iNumeric.equals("46002")) {
				return 1;
			} else if (iNumeric.equals("46001")) {
				return 2;
			} else if (iNumeric.equals("46003")) {
				return 3;
			}

		}
		return 1;

	}
}
 

 

二:在MainActivity 中实现LocationCallBack接口,如下:

 

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.android.location2.MyLocation.LocationCallBack;
public class MainActivity extends Activity implements LocationCallBack{
private TextView desText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        desText = (TextView) this.findViewById(R.id.text);
        new MyLocation(MainActivity.this, this);
}
//回调方法
public void onCurrentLocation(double latitude, double longitude) {
desText.setText("当前经度:" + longitude + "\n当前纬度:"+ latitude);
}
    
}
 

 

三:获取经纬度使用的辅助类,这里补贴代码后面直接上传源码:


四:效果如下图:


 

  • 大小: 3.8 KB
  • 大小: 20.6 KB
分享到:
评论
2 楼 LFHhua104 2015-05-01  
楼主,现在是用不了了么?
1 楼 yanzhiwei147 2012-10-28  
HttpResponse resp = client.execute(post);
这句崩掉了

相关推荐

Global site tag (gtag.js) - Google Analytics