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

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

 更新時(shí)間:2013年06月09日 15:09:49   作者:  
Android 通過當(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 通過用戶當(dāng)前經(jīng)緯度 獲得用戶當(dā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);
  //只是簡單的獲取城市 不需要實(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),它就不會被觸發(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ù)三個(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;
 }

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

相關(guān)文章

  • Android中的Handler與多線程應(yīng)用實(shí)例

    Android中的Handler與多線程應(yīng)用實(shí)例

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

    android點(diǎn)擊無效驗(yàn)證的解決方法

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

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

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

    android實(shí)現(xiàn)一個(gè)圖片驗(yàn)證碼倒計(jì)時(shí)功能

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

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

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

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

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

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

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

    Json 生成與解析詳解及實(shí)例代碼

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

    android 設(shè)置鬧鐘及通知示例

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

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

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

最新評論

南陵县| 搜索| 龙州县| 保靖县| 大同县| 丽水市| 泰宁县| 尤溪县| 洛阳市| 昌黎县| 巢湖市| 平湖市| 松溪县| 新巴尔虎左旗| 绥滨县| 柳河县| 临安市| 苍梧县| 东明县| 临清市| 晋中市| 郎溪县| 绿春县| 台湾省| 丰镇市| 凤山市| 桐城市| 宜丰县| 萨迦县| 桃园县| 宜丰县| 永新县| 彭泽县| 沁源县| 云和县| 和林格尔县| 河东区| 惠安县| 怀远县| 齐齐哈尔市| 肇庆市|