public class LocationUtil {
public static void getLoc(Context context){
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE );
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
final long maxAgeMilliSeconds = 1000 * 60 * 1; // 1 minute
final long maxAgeNetworkMilliSeconds = 1000 * 60 * 10; // 10 minutes
final long now = System.currentTimeMillis();
if(location==null || location.getTime() <now - maxAgeMilliSeconds){
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location == null || location.getTime() < now - maxAgeNetworkMilliSeconds){
//ͨ cellid λ
TelephonyManager telephonyManager = (TelephonyManager)context
.getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation gsmCellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
if (gsmCellLocation != null){
int cid = gsmCellLocation.getCid();
int lac = gsmCellLocation.getLac();
int mnc = Integer.valueOf(telephonyManager.getNetworkOperator().substring(3, 5));
AppContext.currLoc= getLocationByCell(cid, lac, mnc);
}
}else{
// γ ȱ 浽 ڴ
AppContext.currLoc = new LoctionInfo(String.valueOf(location.getLatitude()),String.valueOf(location.getLongitude()));
}
}
}
public static City getCurrCity(Context context){
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE );
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
final long maxAgeMilliSeconds = 1000 * 60 * 1; // 1 minute
final long maxAgeNetworkMilliSeconds = 1000 * 60 * 10; // 10 minutes
final long now = System.currentTimeMillis();
if(location==null || location.getTime() <now - maxAgeMilliSeconds){
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location == null || location.getTime() < now - maxAgeNetworkMilliSeconds){
//ͨ cellid λ
TelephonyManager telephonyManager = (TelephonyManager)context
.getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation gsmCellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
if (gsmCellLocation != null){
int cid = gsmCellLocation.getCid();
int lac = gsmCellLocation.getLac();
int mnc = Integer.valueOf(telephonyManager.getNetworkOperator().substring(3, 5));
AppContext.currLoc= getLocationByCell(cid, lac, mnc);
}
}else{
// γ ȱ 浽 ڴ
AppContext.currLoc = new LoctionInfo(String.valueOf(location.getLatitude()),String.valueOf(location.getLongitude()));
}
}
if(AppContext.currLoc!=null){
context.getSharedPreferences("locInfo", context.MODE_WORLD_WRITEABLE)
.edit().putString("currlat",AppContext.currLoc.getLat())
.putString("currlng",AppContext.currLoc.getLon())
.commit();
// server
String latlng = AppContext.currLoc.getLat()+","+AppContext.currLoc.getLon();
String reqUrl = "http://ditu.google.cn/maps/geo?output=json&key=abcdef&q="+latlng;
City city = sendToServer(reqUrl);
if(city!=null&& city.getCityName()!=null){
// ȡ س
if(AppContext.arrayList!=null){
if(AppContext.arrayList.contains(city)){
City city2 = (City) AppContext.arrayList.get(AppContext.arrayList.indexOf(city));
return city2;
}
}
ResultData resultData;
String cityJson = StringUtil.readCityJson(context);
if(cityJson!=null && !"".equals(cityJson)){
resultData = ParseManger.getCityList(cityJson,context);
}else{
String param = "?ctype=iphone&m=get_citylist";
String cityUrl = APIContants.API+param;
String response = NetManger.doPost(cityUrl, null);
resultData = ParseManger.getCityList(response,context);
}
if(resultData!=null && resultData.getArrayList()!=null){
ArrayList arrayList= resultData.getArrayList();
if(arrayList.contains(city)){
City city2 = (City) arrayList.get(arrayList.indexOf(city));
return city2;
}
}
}
}
return new City("北京", "bei_jing");//如果取不到城市,则默认返回北京
}
public static final String MAPABC_KEY = "4726f4e7aa32db60204e6c05d7e41ada440055e47021072e45322d670324223eedbbd89f7b0788d2";
public static String MAPAPC_INTERFACE_URL = "http://search1.mapabc.com/sisserver";
// config config=BSPS
// mnc mnc= Ӫ ̱ ʶ
// lac lac=λ ñ
// cellid cellid= վID
// a_k a_k=api key û key
/**
* lat, lon
*/
private static LoctionInfo getLocationByCell(int cid, int lac, int mnc) {
StringBuilder url = new StringBuilder(MAPAPC_INTERFACE_URL);
url.append("?config=BSPS&mnc=").append(mnc).append("&lac=").append(lac).append("&cellid=").append(cid).append(
"&a_k=").append(MAPABC_KEY);
String xCoord = "-1";
String yCoord = "-1";
HttpClient httpClient = new DefaultHttpClient();
LogUtil.printInfo("url.toString() : "+url.toString());
HttpGet request = new HttpGet(url.toString());
try {
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(is);
Element root = dom.getDocumentElement();
NodeList personNodes = root.getElementsByTagName("location");
String lat=null,lon=null;
for(int i=0 ; i<personNodes.getLength(); i++){
Element personNode = (Element)personNodes.item(i);
NodeList childNodes = personNode.getChildNodes();
for(int j=0 ; j < childNodes.getLength(); j++){
Node childNode = childNodes.item(j);
if(childNode.getNodeType()==Node.ELEMENT_NODE){
Element element = (Element)childNode;
if("cenx".equals(childNode.getNodeName())){
lon = element.getFirstChild().getNodeValue();
}else if("ceny".equals(childNode.getNodeName())){
lat = element.getFirstChild().getNodeValue();
}
}
}
}
if(lat!=null && lon !=null){
return new LoctionInfo(lat,lon);
}
/*XmlPullParser pullParser = Xml.newPullParser();
pullParser.setInput(is, "GBK");
int eventType = pullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_TAG:
if ("cenx".equals(pullParser.getName())) {
xCoord = pullParser.nextText();
} else if ("ceny".equals(pullParser.getName())) {
yCoord = pullParser.nextText();
}
break;
}
eventType = pullParser.next();
}*/
/*if (!xCoord.equals("-1") && !yCoord.equals("-1")) {
return new LoctionInfo(xCoord,yCoord);
} else {
return null;
}*/
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
public static String convertStreamToString(InputStream is) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"gb2312"));
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
private static City sendToServer(String reqUrl) {
City city=null;
try {
URL url = new URL(reqUrl);
LogUtil.printInfo("reqUrl : "+reqUrl);
String response = NetManger.convertStreamToString(url.openStream());
city = ParseManger.getCurrCity(response);
// LogUtil.printInfo("response : "+response);
// LogUtil.printInfo("city : "+city);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return city;
}
}
分享到:
评论