- 论坛徽章:
- 1
|
主要是当时服务器没给返还经纬度,我是通过百度地图,获取地址转换经纬度,算出距离距离排序,可以美团,大众点评按距离排序相似,如果感觉可以了话希望多点赞
MainActivity.java- package com.example.my;
-
- import java.text.DecimalFormat;
-
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.TextView;
- import android.widget.Toast;
-
- import com.baidu.location.BDLocation;
- import com.baidu.location.BDLocationListener;
- import com.baidu.location.LocationClient;
- import com.baidu.location.LocationClientOption;
- import com.baidu.mapapi.SDKInitializer;
- import com.baidu.mapapi.map.BaiduMap;
- import com.baidu.mapapi.map.MapStatusUpdateFactory;
- import com.baidu.mapapi.map.MapView;
- import com.baidu.mapapi.search.core.SearchResult;
- import com.baidu.mapapi.search.geocode.GeoCodeOption;
- import com.baidu.mapapi.search.geocode.GeoCodeResult;
- import com.baidu.mapapi.search.geocode.GeoCoder;
- import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
- import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
- import com.example.mybaidumap.R;
-
- public class MainActivity extends Activity implements OnGetGeoCoderResultListener {
- private static final double EARTH_RADIUS = 6378.137;
- boolean isFirstLoc = true;
- private String currentAddress;
-
- private TextView conaddress;
-
- GeoCoder mSearch = null; // 搜索模块,也可去掉地图模块独立使用
- BaiduMap mBaiduMap = null;
- MapView mMapView = null;
- private double longitude;
- private double latitude;
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 在使用SDK各组件之前初始化context信息,传入ApplicationContext
- SDKInitializer.initialize(getApplicationContext());
- setContentView(R.layout.activity_jw_main);
- init();
- }
-
- private void init() {
-
- conaddress=(TextView) findViewById(R.id.conaddress);
-
- // ss = (TextView) findViewById(R.id.ss);
- // 获取地图控件引用
- //mMapView = (MapView) findViewById(R.id.bmapView);
- //mBaiduMap = mMapView.getMap();
-
- // 设置地图初始Zoom大小
- //mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(15.0f));
-
- // 定位初始化
- LocationClient mLocClient = new LocationClient(this);
- MyLocationListenner myListener = new MyLocationListenner();
- mLocClient.registerLocationListener(myListener);
-
- LocationClientOption option = new LocationClientOption();
- option.setOpenGps(true);// 打开gps
- option.setCoorType("bd09ll"); // 设置坐标类型
- option.setScanSpan(1000);
- option.setAddrType("all");
- mLocClient.setLocOption(option);
- mLocClient.start();
- CharSequence titleLable = "地理编码功能";
- setTitle(titleLable);
-
- // 初始化搜索模块,注册事件监听
- mSearch = GeoCoder.newInstance();
- mSearch.setOnGetGeoCodeResultListener(this);
-
- findViewById(R.id.geocode).setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
- // Geo搜索
- mSearch.geocode(new GeoCodeOption().city("").address("太阳宫新纪园便民洗车房,朝阳区,望京,北京"));
- }
- });
-
- }
-
- @Override
- protected void onDestroy() {
- super.onDestroy();
- // 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
- //mMapView.onDestroy();
- //mSearch.destroy();
- }
-
- @Override
- protected void onResume() {
- super.onResume();
- // 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
- //mMapView.onResume();
- }
-
- @Override
- protected void onPause() {
- super.onPause();
- // 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
- //mMapView.onPause();
- }
-
- /**
- * 定位SDK监听函数
- */
- public class MyLocationListenner implements BDLocationListener {
-
- @SuppressLint("NewApi")
- @Override
- public void onReceiveLocation(BDLocation location) {
- // map view 销毁后不在处理新接收的位置
- /*if (location == null || mMapView == null)
- return;
- address = location.getAddrStr();
- */ if (isFirstLoc) {
- isFirstLoc = false;
-
- //获取定位的详细地址
- currentAddress = location.getAddrStr();
- //获取市之前的地址
- //if(!address=null){
- String addres=currentAddress.substring(0, currentAddress.lastIndexOf('市'));
- conaddress.setText(addres);
- //}
-
- StringBuffer sb = new StringBuffer(256);
- sb.append("time : ");
- sb.append(location.getTime());
- sb.append("\nwd : ");
- sb.append(location.getLatitude());
- sb.append("\nj d : ");
- sb.append(location.getLongitude());
- sb.append("\nradius : ");
- sb.append(location.getRadius());
-
- longitude = location.getLongitude();
- latitude = location.getLatitude();
-
- System.out.println("location " + "经度 = " + longitude
- + "纬度 = " + latitude );
-
- Toast.makeText(getApplicationContext(), location.getAddrStr(), Toast.LENGTH_LONG).show();
-
- }
- }
-
-
-
- public void SearchButtonProcess(View v) {
- mSearch.geocode(new GeoCodeOption().address("西城区大成广场"
- ));
-
- }
-
- }
-
-
-
- //计算两者之间的距离
- public double calculateJWD(double lng_a, double lat_a, double lng_b, double lat_b) {
- double radLat1 = (lat_a * Math.PI / 180.0);
- double radLat2 = (lat_b * Math.PI / 180.0);
- double a = radLat1 - radLat2;
- double b = (lng_a - lng_b) * Math.PI / 180.0;
- double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
- + Math.cos(radLat1) * Math.cos(radLat2)
- * Math.pow(Math.sin(b / 2), 2)));
- s = s * EARTH_RADIUS;
- s = Math.round(s * 10000) / 10000;
- System.out.println("is name Math.round------>"+ s);
- //int jw = (int) (s*1000);
- // System.out.println("is name Math.round------>"+ jw);
-
- DecimalFormat df = new DecimalFormat("#######0");
- String dfs = df.format(s);
-
- //ss.setText("距离:"+dfs+"米");
-
- return s;
- }
-
-
- @Override
- public void onGetGeoCodeResult(GeoCodeResult result) {
- if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
- Toast.makeText(MainActivity.this, "抱歉,未能找到结果", Toast.LENGTH_LONG)
- .show();
- return;
- }
-
- //拿到经纬度
- String strInfo = String.format("纬度:%f 经度:%f",
- result.getLocation().latitude, result.getLocation().longitude);
-
- //算距离
- double calculateJWD = calculateJWD(longitude,latitude, result.getLocation().longitude, result.getLocation().latitude);
-
- Toast.makeText(MainActivity.this, strInfo, Toast.LENGTH_SHORT).show();
-
- Toast.makeText(MainActivity.this, calculateJWD+"", Toast.LENGTH_LONG).show();
-
-
- }
-
- @Override
- public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
- if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
- Toast.makeText(MainActivity.this, "抱歉,未能找到结果", Toast.LENGTH_LONG)
- .show();
- return;
- }
-
- }
- }
复制代码 activity_jw_main.xml- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" >
-
- <!-- <com.baidu.mapapi.map.MapView
- android:id="@+id/bmapView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:visibility="invisible"
- android:clickable="true" /> -->
-
- <TextView
- android:id="@+id/conaddress"
- android:layout_width="fill_parent"
- android:gravity="left|center_vertical"
- android:paddingLeft="10dp"
- android:layout_height="30dp"
- android:textSize="13sp"
- android:background="#45000000"
- android:textColor="#FFFFFF"
- />
-
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_below="@+id/conaddress"
- android:layout_marginTop="19dp"
- android:orientation="horizontal" >
-
- <Button
- android:id="@+id/geocode"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/button_style"
- android:text="Geo" />
- </LinearLayout>
-
- </RelativeLayout>
复制代码 |
|