高德完美获取地理位置信息并显示出来~~

// 获取地理位置
 private LocationManagerProxy locationManager = null;
 private static final long mLocationUpdateMinTime = 1000;
 private static final float mLocationUpdateMinDistance = 10.0f;
 private LocationHandler locationhandler;
 private String strdata;
 // 纬度
 private Double geoLat;
 // 经度
 private Double geoLng;

 

locationManager = new LocationManagerProxy(
      SendWbActivity.this,getResources().getString(R.string.map_api));

locationhandler = new LocationHandler();
    Thread locationThread = new LocationThread();
    locationThread.start();

 

class LocationThread extends Thread
 {

  @Override
  public void run()
  {
   // TODO Auto-generated method stub
   super.run();
   Looper.prepare();
   // locationManager = new LocationManagerProxy(SendWbActivity.this,
   // getResources().getString(R.string.map_api));
   // TODO Auto-generated method stub
   for (final String provider : locationManager.getProviders(true))
   {
    Log.i(TAG, "开启异步线程获取地理位置!");
    if (LocationManagerProxy.GPS_PROVIDER.equals(provider)
      || LocationProviderProxy.MapABCNetwork.equals(provider))
    {
     Log.i(TAG, "开启线程~~~");
     locationManager.requestLocationUpdates(provider,
       mLocationUpdateMinTime, mLocationUpdateMinDistance,
       locationListener);
    }
   }
   Looper.loop();
  }

 }

 LocationListener locationListener = new LocationListener()
 {

  @Override
  public void onProviderDisabled(String provider)
  {
  }

  @Override
  public void onProviderEnabled(String provider)
  {
  }

  @Override
  public void onStatusChanged(String provider, int status, Bundle extras)
  {
  }

  @Override
  public void onLocationChanged(Location location)
  {
   // TODO Auto-generated method stub

   // TODO Auto-generated method stub
   if (location != null)
   {

    geoLat = location.getLatitude();
    geoLng = location.getLongitude();
    // 经度:longitude 纬度:latitude
    /*
     * lnglat 经纬度值,采用经度在前,纬度在后,经纬度值之间用“,”隔开的方式
     * 如:113.94830703735352,22.54046422124227 reqsrc 请求来源,请填写"wb”
     */
    Log.i(TAG, "开始获取地理位置~~");
    String result = null;
    String lnglat = geoLng + "," + geoLat;
    Map<String, String> map = new HashMap<String, String>();
    map.put("lnglat", lnglat);
    map.put("reqsrc", "wb");

    String signUrl = UrlUtil.buildUrlByQueryStringMapAndBaseUrl(
      Constants.MAP_RGEOC, map);
    HttpGet httpGet = new HttpGet(signUrl);
    HttpResponse httpResponse = null;
    HttpClient httpClient = new DefaultHttpClient();
    try
    {
     httpResponse = httpClient.execute(httpGet);
     result = EntityUtils.toString(httpResponse.getEntity(),
       "GB2312");
     JSONObject dataObj = new JSONObject(result)
       .getJSONObject("detail");
     JSONArray array = dataObj.getJSONArray("poilist");
     for (int i = 0; i < array.length(); i++)
     {

      System.out.println("array:" + array.getString(i));
     }
     JSONObject data = array.getJSONObject(1);
     strdata = data.getString("addr");
     System.out.println("Str:" + strdata);

     Log.i(TAG, "地理位置为:" + strdata);

     Toast.makeText(SendWbActivity.this, strdata,
       Toast.LENGTH_LONG).show();

     Message msg = locationhandler.obtainMessage();
     locationhandler.sendMessage(msg);
    } catch (Exception e)
    {
     e.printStackTrace();
    }
    System.out.println("地址位置:" + result);
   }

  }

 };

 class LocationHandler extends Handler
 {
  @Override
  public void handleMessage(Message msg)
  {
   // TODO Auto-generated method stub
   super.handleMessage(msg);

   location_image.setVisibility(View.VISIBLE);
   location_progressBar.setVisibility(View.GONE);
   location_text.setText(strdata);
   locationManager.removeUpdates(locationListener);

  }

 }

 

R.string.map_api在高德官方申请的授权码,http://code.mapabc.com/在这里申请~~~

或得经纬度后,将经纬度上传到腾讯地图,返回JSON数据,解析就OK了~~~

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值