免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2202 | 回复: 0
打印 上一主题 下一主题

百度sdk定位当前的位置和经纬度, [复制链接]

论坛徽章:
1
操作系统版块每日发帖之星
日期:2015-07-07 22:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-06 14:19 |只看该作者 |倒序浏览
主要是当时服务器没给返还经纬度,我是通过百度地图,获取地址转换经纬度,算出距离距离排序,可以美团,大众点评按距离排序相似,如果感觉可以了话希望多点赞
MainActivity.java
  1. package com.example.my;

  2. import java.text.DecimalFormat;

  3. import android.annotation.SuppressLint;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.TextView;
  9. import android.widget.Toast;

  10. import com.baidu.location.BDLocation;
  11. import com.baidu.location.BDLocationListener;
  12. import com.baidu.location.LocationClient;
  13. import com.baidu.location.LocationClientOption;
  14. import com.baidu.mapapi.SDKInitializer;
  15. import com.baidu.mapapi.map.BaiduMap;
  16. import com.baidu.mapapi.map.MapStatusUpdateFactory;
  17. import com.baidu.mapapi.map.MapView;
  18. import com.baidu.mapapi.search.core.SearchResult;
  19. import com.baidu.mapapi.search.geocode.GeoCodeOption;
  20. import com.baidu.mapapi.search.geocode.GeoCodeResult;
  21. import com.baidu.mapapi.search.geocode.GeoCoder;
  22. import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
  23. import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
  24. import com.example.mybaidumap.R;

  25. public class MainActivity extends Activity implements OnGetGeoCoderResultListener {
  26.     private static final double EARTH_RADIUS = 6378.137;
  27.     boolean isFirstLoc = true;
  28.     private String currentAddress;
  29.      
  30.     private TextView conaddress;
  31.      
  32.     GeoCoder mSearch = null; // 搜索模块,也可去掉地图模块独立使用
  33.     BaiduMap mBaiduMap = null;
  34.     MapView mMapView = null;
  35.     private double longitude;
  36.     private double latitude;

  37.      
  38.     @Override
  39.     protected void onCreate(Bundle savedInstanceState) {
  40.         super.onCreate(savedInstanceState);
  41.         // 在使用SDK各组件之前初始化context信息,传入ApplicationContext
  42.         SDKInitializer.initialize(getApplicationContext());
  43.         setContentView(R.layout.activity_jw_main);
  44.         init();
  45.     }

  46.     private void init() {
  47.          
  48.          conaddress=(TextView) findViewById(R.id.conaddress);
  49.          
  50.         // ss = (TextView) findViewById(R.id.ss);
  51.         // 获取地图控件引用
  52.        //mMapView = (MapView) findViewById(R.id.bmapView);
  53.         //mBaiduMap = mMapView.getMap();
  54.          
  55.         // 设置地图初始Zoom大小
  56.         //mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(15.0f));
  57.          
  58.         // 定位初始化
  59.          LocationClient mLocClient = new LocationClient(this);
  60.          MyLocationListenner myListener = new MyLocationListenner();
  61.          mLocClient.registerLocationListener(myListener);
  62.          
  63.         LocationClientOption option = new LocationClientOption();
  64.         option.setOpenGps(true);// 打开gps
  65.         option.setCoorType("bd09ll"); // 设置坐标类型
  66.         option.setScanSpan(1000);
  67.         option.setAddrType("all");
  68.         mLocClient.setLocOption(option);
  69.         mLocClient.start();
  70.         CharSequence titleLable = "地理编码功能";
  71.         setTitle(titleLable);

  72.         // 初始化搜索模块,注册事件监听
  73.         mSearch = GeoCoder.newInstance();
  74.         mSearch.setOnGetGeoCodeResultListener(this);
  75.          
  76.         findViewById(R.id.geocode).setOnClickListener(new OnClickListener() {
  77.             
  78.             @Override
  79.             public void onClick(View arg0) {
  80.                 // Geo搜索
  81.                 mSearch.geocode(new GeoCodeOption().city("").address("太阳宫新纪园便民洗车房,朝阳区,望京,北京"));
  82.             }
  83.         });
  84.          
  85.     }

  86.     @Override
  87.     protected void onDestroy() {
  88.         super.onDestroy();
  89.         // 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
  90.         //mMapView.onDestroy();
  91.         //mSearch.destroy();
  92.     }

  93.     @Override
  94.     protected void onResume() {
  95.         super.onResume();
  96.         // 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
  97.         //mMapView.onResume();
  98.     }

  99.     @Override
  100.     protected void onPause() {
  101.         super.onPause();
  102.         // 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
  103.         //mMapView.onPause();
  104.     }
  105.      
  106.     /**
  107.      * 定位SDK监听函数
  108.      */
  109.     public class MyLocationListenner implements BDLocationListener {
  110.          
  111.         @SuppressLint("NewApi")
  112.         @Override
  113.         public void onReceiveLocation(BDLocation location) {
  114.             // map view 销毁后不在处理新接收的位置
  115.             /*if (location == null || mMapView == null)
  116.                 return;
  117.              address = location.getAddrStr();
  118. */          if (isFirstLoc) {
  119.                 isFirstLoc = false;
  120.             
  121.                 //获取定位的详细地址
  122.                 currentAddress = location.getAddrStr();
  123.                 //获取市之前的地址
  124.                 //if(!address=null){
  125.                 String addres=currentAddress.substring(0, currentAddress.lastIndexOf('市'));
  126.                 conaddress.setText(addres);
  127.                 //}
  128.                  
  129.                 StringBuffer sb = new StringBuffer(256);
  130.                 sb.append("time : ");
  131.                 sb.append(location.getTime());
  132.                 sb.append("\nwd : ");
  133.                 sb.append(location.getLatitude());
  134.                 sb.append("\nj d : ");
  135.                 sb.append(location.getLongitude());
  136.                 sb.append("\nradius : ");
  137.                 sb.append(location.getRadius());
  138.                  
  139.                 longitude = location.getLongitude();
  140.                 latitude = location.getLatitude();

  141.                 System.out.println("location " + "经度 = " + longitude
  142.                         + "纬度 = " + latitude );
  143.                  
  144.                 Toast.makeText(getApplicationContext(), location.getAddrStr(), Toast.LENGTH_LONG).show();
  145.                  
  146.             }
  147.         }
  148.          
  149.      
  150.                
  151.         public void SearchButtonProcess(View v) {
  152.                 mSearch.geocode(new GeoCodeOption().address("西城区大成广场"
  153.                         ));
  154.             
  155.             }
  156.          
  157.         }


  158.      
  159.             //计算两者之间的距离
  160.             public double calculateJWD(double lng_a, double lat_a, double lng_b, double lat_b) {
  161.             double radLat1 = (lat_a * Math.PI / 180.0);
  162.             double radLat2 = (lat_b * Math.PI / 180.0);
  163.             double a = radLat1 - radLat2;
  164.             double b = (lng_a - lng_b) * Math.PI / 180.0;
  165.             double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
  166.                             + Math.cos(radLat1) * Math.cos(radLat2)
  167.                             * Math.pow(Math.sin(b / 2), 2)));
  168.             s = s * EARTH_RADIUS;
  169.             s = Math.round(s * 10000) / 10000;
  170.             System.out.println("is name Math.round------>"+ s);
  171.             //int jw = (int) (s*1000);
  172.            // System.out.println("is name Math.round------>"+ jw);
  173.             
  174.             DecimalFormat df = new DecimalFormat("#######0");
  175.             String dfs = df.format(s);
  176.             
  177.             //ss.setText("距离:"+dfs+"米");
  178.             
  179.             return s;
  180.         }
  181.      
  182.      
  183.     @Override
  184.     public void onGetGeoCodeResult(GeoCodeResult result) {
  185.         if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
  186.             Toast.makeText(MainActivity.this, "抱歉,未能找到结果", Toast.LENGTH_LONG)
  187.                     .show();
  188.             return;
  189.         }
  190.          
  191.         //拿到经纬度
  192.         String strInfo = String.format("纬度:%f 经度:%f",
  193.                 result.getLocation().latitude, result.getLocation().longitude);
  194.          
  195.         //算距离
  196.         double calculateJWD = calculateJWD(longitude,latitude, result.getLocation().longitude, result.getLocation().latitude);
  197.          
  198.         Toast.makeText(MainActivity.this, strInfo, Toast.LENGTH_SHORT).show();
  199.          
  200.         Toast.makeText(MainActivity.this, calculateJWD+"", Toast.LENGTH_LONG).show();
  201.          
  202.      
  203.     }

  204.     @Override
  205.     public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
  206.         if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
  207.             Toast.makeText(MainActivity.this, "抱歉,未能找到结果", Toast.LENGTH_LONG)
  208.                     .show();
  209.             return;
  210.         }
  211.      
  212.     }
  213. }
复制代码
activity_jw_main.xml
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     tools:context=".MainActivity" >
  6.      
  7.   <!-- <com.baidu.mapapi.map.MapView  
  8.     android:id="@+id/bmapView"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="fill_parent"  
  11.     android:visibility="invisible"
  12.     android:clickable="true" /> -->

  13.         <TextView
  14.         android:id="@+id/conaddress"
  15.         android:layout_width="fill_parent"
  16.         android:gravity="left|center_vertical"
  17.         android:paddingLeft="10dp"
  18.         android:layout_height="30dp"
  19.         android:textSize="13sp"
  20.         android:background="#45000000"
  21.         android:textColor="#FFFFFF"
  22.         />

  23.               <LinearLayout
  24.                   android:layout_width="fill_parent"
  25.                   android:layout_height="wrap_content"
  26.                   android:layout_alignParentLeft="true"
  27.                   android:layout_below="@+id/conaddress"
  28.                   android:layout_marginTop="19dp"
  29.                   android:orientation="horizontal" >

  30.                   <Button
  31.                       android:id="@+id/geocode"
  32.                       android:layout_width="wrap_content"
  33.                       android:layout_height="wrap_content"
  34.                       android:background="@drawable/button_style"
  35.                       android:text="Geo" />
  36.               </LinearLayout>

  37. </RelativeLayout>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP