最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 通過當前經(jīng)緯度獲得城市的實例代碼

 更新時間:2013年06月09日 15:09:49   作者:  
Android 通過當前經(jīng)緯度獲得城市的實例代碼,需要的朋友可以參考一下
復制代碼 代碼如下:

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 通過用戶當前經(jīng)緯度 獲得用戶當前城市
  */
 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) {
//      //當坐標改變時觸發(fā)此函數(shù),如果Provider傳進相同的坐標,它就不會被觸發(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(關閉)"+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)在可用、" +
//        "暫時不可用和無服務三個狀態(tài)直接切換時觸發(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;
 }

 /**
  * 通過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中的Handler與多線程應用實例

    Android中的Handler與多線程應用實例

    這篇文章主要介紹了Android中的Handler與多線程應用實例,本文首先解釋一下handler是用來干嘛的,然后通過例子介紹其在多線程中的應用,需要的朋友可以參考下
    2015-03-03
  • android點擊無效驗證的解決方法

    android點擊無效驗證的解決方法

    這篇文章主要給大家介紹了關于android點擊無效驗證的解決方法,文中通過示例代碼介紹的非常詳細,對各位android開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-12-12
  • Android判斷和監(jiān)聽底座狀態(tài)和類型的方法介紹

    Android判斷和監(jiān)聽底座狀態(tài)和類型的方法介紹

    這篇文章主要介紹了Android判斷和監(jiān)聽底座狀態(tài)和類型的方法介紹,例如判斷當前底座狀態(tài)、判斷插入底座類型、監(jiān)控充電充電狀態(tài)等,需要的朋友可以參考下
    2014-06-06
  • android實現(xiàn)一個圖片驗證碼倒計時功能

    android實現(xiàn)一個圖片驗證碼倒計時功能

    本文通過實例代碼給大家介紹了android實現(xiàn)一個圖片驗證碼倒計時功能,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2017-11-11
  • Android中WebView控件支持地理位置定位方法

    Android中WebView控件支持地理位置定位方法

    今天小編就為大家分享一篇Android中WebView控件支持地理位置定位方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Android自定義SeekBar實現(xiàn)滑動驗證且不可點擊

    Android自定義SeekBar實現(xiàn)滑動驗證且不可點擊

    這篇文章主要為大家詳細介紹了Android自定義SeekBar實現(xiàn)滑動驗證且不可點擊,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • Android開發(fā)之菜單(menu)用法實例分析

    Android開發(fā)之菜單(menu)用法實例分析

    這篇文章主要介紹了Android開發(fā)之菜單(menu)用法,結(jié)合實例形式較為詳細的分析了Android菜單的實現(xiàn)步驟與相關操作技巧,需要的朋友可以參考下
    2017-03-03
  • Json 生成與解析詳解及實例代碼

    Json 生成與解析詳解及實例代碼

    這篇文章主要介紹了Json 生成與解析詳解及實例代碼的相關資料,這里附簡單實例幫助大家學習理解,需要的朋友可以參考下
    2016-11-11
  • android 設置鬧鐘及通知示例

    android 設置鬧鐘及通知示例

    本篇文章主要介紹了android 設置鬧鐘及通知示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • Android實現(xiàn)的仿淘寶購物車demo示例

    Android實現(xiàn)的仿淘寶購物車demo示例

    這篇文章主要介紹了Android實現(xiàn)的仿淘寶購物車demo示例,結(jié)合實例形式分析了Android購物車的功能、布局及邏輯實現(xiàn)技巧,需要的朋友可以參考下
    2016-07-07

最新評論

台北县| 临沧市| 高雄县| 海淀区| 绍兴县| 中卫市| 锡林郭勒盟| 垣曲县| 青田县| 庆元县| 泗阳县| 沙湾县| 柯坪县| 渭源县| 元氏县| 霍州市| 乐山市| 徐闻县| 星子县| 镇江市| 鄂托克旗| 南澳县| 尉氏县| 娄底市| 贺州市| 青州市| 泰宁县| 新晃| 万宁市| 肇庆市| 牙克石市| 昌平区| 远安县| 丰县| 商洛市| 开封县| 景宁| 柳江县| 射阳县| 封开县| 合水县|