Android 通過(guò)當(dāng)前經(jīng)緯度獲得城市的實(shí)例代碼
更新時(shí)間:2013年06月09日 15:09:49 作者:
Android 通過(guò)當(dāng)前經(jīng)緯度獲得城市的實(shí)例代碼,需要的朋友可以參考一下
復(fù)制代碼 代碼如下:
package com.yy;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
public class GetCity {
/**
* 借助Google MAP 通過(guò)用戶當(dāng)前經(jīng)緯度 獲得用戶當(dāng)前城市
*/
static final String GOOGLE_MAPS_API_KEY = "abcdefg";
private LocationManager locationManager;
private Location currentLocation;
private String city="全國(guó)";
public GetCity(Context context) {
this.locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
//只是簡(jiǎn)單的獲取城市 不需要實(shí)時(shí)更新 所以這里先注釋
// this.locationManager.requestLocationUpdates(
// LocationManager.GPS_PROVIDER, 1000, 0,
// new LocationListener() {
// public void onLocationChanged(Location loc) {
// //當(dāng)坐標(biāo)改變時(shí)觸發(fā)此函數(shù),如果Provider傳進(jìn)相同的坐標(biāo),它就不會(huì)被觸發(fā)
// // Save the latest location
// currentLocation = loc;
// // Update the latitude & longitude TextViews
// System.out
// .println("getCity()"
// + (loc.getLatitude() + " " + loc
// .getLongitude()));
// }
//
// public void onProviderDisabled(String arg0) {
// System.out.println(".onProviderDisabled(關(guān)閉)"+arg0);
// }
//
// public void onProviderEnabled(String arg0) {
// System.out.println(".onProviderEnabled(開(kāi)啟)"+arg0);
// }
//
// public void onStatusChanged(String arg0, int arg1,
// Bundle arg2) {
// System.out.println(".onStatusChanged(Provider的轉(zhuǎn)態(tài)在可用、" +
// "暫時(shí)不可用和無(wú)服務(wù)三個(gè)狀態(tài)直接切換時(shí)觸發(fā)此函數(shù))"+
// arg0+" "+arg1+" "+arg2);
// }
// });
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (currentLocation == null)
currentLocation = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
/**
* 開(kāi)始解析
*/
public void start() {
if(currentLocation!=null){
new Thread(){
public void run(){
String temp=reverseGeocode(currentLocation);
if(temp!=null&&temp.length()>=2)
city=temp;
}
}.start();
}else{
System.out.println("GetCity.start()未獲得location");
}
}
/**
* 獲得城市
* @return
*/
public String getCity(){
return city;
}
/**
* 通過(guò)Google map api 解析出城市
* @param loc
* @return
*/
public String reverseGeocode(Location loc) {
// http://maps.google.com/maps/geo?q=40.714224,-73.961452&output=json&oe=utf8&sensor=true_or_false&key=your_api_key
String localityName = "";
HttpURLConnection connection = null;
URL serverAddress = null;
try {
// build the URL using the latitude & longitude you want to lookup
// NOTE: I chose XML return format here but you can choose something
// else
serverAddress = new URL("http://maps.google.com/maps/geo?q="
+ Double.toString(loc.getLatitude()) + ","
+ Double.toString(loc.getLongitude())
+ "&output=xml&oe=utf8&sensor=true&key="
+ GOOGLE_MAPS_API_KEY);
// set up out communications stuff
connection = null;
// Set up the initial connection
connection = (HttpURLConnection) serverAddress.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setReadTimeout(10000);
connection.connect();
try {
InputStreamReader isr = new InputStreamReader(connection
.getInputStream());
InputSource source = new InputSource(isr);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader xr = parser.getXMLReader();
GoogleReverseGeocodeXmlHandler handler = new GoogleReverseGeocodeXmlHandler();
xr.setContentHandler(handler);
xr.parse(source);
localityName = handler.getLocalityName();
System.out.println("GetCity.reverseGeocode()"+localityName);
} catch (Exception ex) {
ex.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("GetCity.reverseGeocode()"+ex);
}
return localityName;
}
/**
* The final piece of this puzzle is parsing the xml tha
您可能感興趣的文章:
- Android通過(guò)原生方式獲取經(jīng)緯度與城市信息的方法
- Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo)
- Android通過(guò)原生APi獲取所在位置的經(jīng)緯度
- Android編程實(shí)現(xiàn)根據(jù)經(jīng)緯度查詢地址并對(duì)獲取的json數(shù)據(jù)進(jìn)行解析的方法
- Android獲取當(dāng)前位置的經(jīng)緯度數(shù)據(jù)
- android通過(guò)gps獲取定位的位置數(shù)據(jù)和gps經(jīng)緯度
- Android獲取經(jīng)緯度計(jì)算距離介紹
- android手機(jī)獲取gps和基站的經(jīng)緯度地址實(shí)現(xiàn)代碼
- Android獲取經(jīng)緯度的完美解決方案
相關(guān)文章
Android Studio 運(yùn)行時(shí)出現(xiàn)的警告信息解決辦法
這篇文章主要介紹了Android Studio 運(yùn)行時(shí)出現(xiàn)的警告信息解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06Flutter插件開(kāi)發(fā)之HmsScanKit實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Flutter插件開(kāi)發(fā)之HmsScanKit實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android中顯示GIF動(dòng)畫(huà)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android中顯示GIF動(dòng)畫(huà)的實(shí)現(xiàn)代碼,較為詳細(xì)的分析了Android調(diào)用GIF動(dòng)畫(huà)所涉及的頁(yè)面布局及功能實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android編程之微信SDK分享功能過(guò)程步驟詳細(xì)分析
這篇文章主要介紹了Android編程之微信SDK分享功能過(guò)程步驟詳細(xì)分析,較為詳細(xì)的分析了Android微信SDK分享功能的原理、步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2015-10-10android基礎(chǔ)總結(jié)篇之二:Activity的四種launchMode
這篇文章主要介紹了android基礎(chǔ)總結(jié)篇之二:Activity的四種launchMode,有需要的可以了解一下。2016-11-11Android WebView 常見(jiàn)問(wèn)題及處理方案
這篇文章主要介紹了Android WebView 常見(jiàn)問(wèn)題及處理方案,需要的朋友可以參考下2015-08-08Android通過(guò)自定義View實(shí)現(xiàn)隨機(jī)驗(yàn)證碼
這篇文章主要介紹了Android通過(guò)自定義View實(shí)現(xiàn)隨機(jī)驗(yàn)證碼的相關(guān)資料,需要的朋友可以參考下2016-03-03