国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android 通過(guò)當(dāng)前經(jīng)緯度獲得城市的實(shí)例代碼

2020-04-11 12:13:25
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
復(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(開啟)"+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);
 }
 /**
  * 開始解析
  */
 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
 
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 榆社县| 贵州省| 武穴市| 神池县| 瓮安县| 宁国市| 亚东县| 通化市| 长顺县| 广元市| 宁蒗| 青阳县| 依安县| 西乌珠穆沁旗| 兴海县| 德兴市| 平度市| 淄博市| 抚顺市| 永寿县| 田阳县| 麟游县| 桦甸市| 太和县| 河间市| 桂东县| 安溪县| 昆明市| 哈巴河县| 五寨县| 兴和县| 正定县| 龙江县| 玉树县| 长沙县| 辽宁省| 义乌市| 兴宁市| 景洪市| 黔江区| 岑巩县|