a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

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

2020-04-11 12:13:25
字體:
來源:轉載
供稿:網友
復制代碼 代碼如下:

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
 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美精品日韩 | 欧美精品一区二区视频 | 亚洲一区二区三 | 日日干日日操 | 在线成人免费观看www | 毛片在线免费播放 | 91精品国产综合久久久久久漫画 | 美女视频一区 | 日韩在线免费观看网站 | 成人黄色a | 午夜影院在线看 | 久久91精品国产 | 日韩激情网 | 中文字幕国产 | 精品国自产在线观看 | 综合网激情五月 | 欧美一级在线观看 | 久艹伊人 | 久久久久av | 成人免费视频一区二区 | 日韩精品一区二区三区中文在线 | 成人做爰9片免费视频 | 国内精品久久久久久久影视蜜臀 | 一区二区三区回区在观看免费视频 | 成人精品一区 | 久久国产精品大桥未久av | 欧美视频在线一区 | 男人操女人bb | 成人欧美一区二区三区在线观看 | 久久国产精品一区二区 | 欧美日韩一区不卡 | 久久久国产一区二区三区 | av日韩在线播放 | 欧美大片在线看免费观看 | 午夜网址| 久久久亚洲一区 | 色婷婷一区二区三区四区 | 国产成人99久久亚洲综合精品 | 国产精品一区二区三区免费视频 | 欧美一区二区三区啪啪 | 久久精品成人免费视频 |