欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-04 14:36:34
复制代码 代码如下:package com.yy; import java.io.inputstreamreader;import java.net.httpurlconn...
复制代码 代码如下:

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