一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android 通過當前經緯度獲得城市的實例代碼

Android 通過當前經緯度獲得城市的實例代碼

2021-01-27 14:32Android開發網 Android

Android 通過當前經緯度獲得城市的實例代碼,需要的朋友可以參考一下

復制代碼 代碼如下:


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 通過用戶當前經緯度 獲得用戶當前城市
  */
 static final String GOOGLE_MAPS_API_KEY = "abcdefg";

 private LocationManager locationManager;
 private Location currentLocation;
 private String city="全國";
 public GetCity(Context context) {
  this.locationManager = (LocationManager) context
    .getSystemService(Context.LOCATION_SERVICE);
  //只是簡單的獲取城市 不需要實時更新 所以這里先注釋
//  this.locationManager.requestLocationUpdates(
//    LocationManager.GPS_PROVIDER,  1000, 0,
//    new LocationListener() {
//     public void onLocationChanged(Location loc) {
//      //當坐標改變時觸發此函數,如果Provider傳進相同的坐標,它就不會被觸發
//      // 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(關閉)"+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的轉態在可用、" +
//        "暫時不可用和無服務三個狀態直接切換時觸發此函數)"+
//        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;
 }

 /**
  * 通過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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天天乐影院 | 亚洲国产精品综合欧美 | 日韩欧美亚洲天堂 | 99精品热 | 性吟网 | 国产射频放大器 | 青青精品视频 | 国产成人理在线观看视频 | 国产成人精品系列在线观看 | 秋霞综合网 | 2022超帅男同gayxxx | 成人永久免费视频 | 好大好硬好深好爽想要之黄蓉 | 欧美性色黄大片四虎影视 | 四虎成人www国产精品 | 久久亚洲精品专区蓝色区 | 欧美日韩精品亚洲精品v18 | 国产在线98福利播放视频免费 | 成人资源影音先锋久久资源网 | 1769国产精品免费视频 | chinesexxxxhd人妖 chinesespanking调教 | 国产色司机在线视频免费观看 | 高清欧美不卡一区二区三区 | 美女视频一区二区三区在线 | 26uuu老色哥 236宅宅2021最新理论 | 石原莉奈被店长侵犯免费 | 九二淫黄大片看片 | 美女被的视频 | 日本色频 | 999精品视频在线观看热6 | 国内视频一区二区三区 | 国产一区私人高清影院 | 亚洲精品资源 | 欧美国产日本高清不卡 | 亚洲欧洲日产国码天堂 | 暖暖高清日本在线 | 视频网站入口在线看 | 婷婷综合缴情亚洲五月伊 | 精品国产自在现线拍国语 | 四虎最新永久免费视频 | 91久久偷偷做嫩草影院免费 |