Android 通过当前经纬度获得城市的实例代码

所属分类: 软件编程 / Android 阅读数: 1044
收藏 0 赞 0 分享
复制代码 代码如下:

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
 
更多精彩内容其他人还在看

Android实现信号强度监听的方法

这篇文章主要介绍了Android实现信号强度监听的方法,是Android手机中很常见的一个实用功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Android实现Activity界面切换添加动画特效的方法

这篇文章主要介绍了Android实现Activity界面切换添加动画特效的方法,非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中Dialog去黑边的方法

这篇文章主要介绍了Android中Dialog去黑边的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Qt for Android开发实例教程

这篇文章主要介绍了Qt for Android开发的方法,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之时间日期操作实例

这篇文章主要介绍了Android开发之时间日期操作,是Android程序开发中常见的一个功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之时间日期组件用法实例

这篇文章主要介绍了Android开发之时间日期组件用法,主要介绍了TimePicker和DatePicker组件,对于Android程序开发有不错的借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之获取网络链接状态

这篇文章主要介绍了Android获取网络链接状态的方法,主要是通过ConnectivityManager类来完成的,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之广播机制浅析

这篇文章主要介绍了Android开发之广播机制浅析,主要包括了发布、接收及配置广播的实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之登录验证实例教程

这篇文章主要介绍了Android开发之登录验证实现方法,包括发送数据、服务器端验证、配置文件等,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之注册登录方法示例

这篇文章主要介绍了Android开发的注册登录方法,是针对Android程序设计中版本兼容性的进一步完善,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多