赞
踩
百度定位相信大家都会使用,作为一个一年经验的安卓经验的新人,我也就不多说什么了.今天要给大家带来的是一个模糊定位,一个小小的需求,就是根据自己的定位地点的经纬度,解析旁边城市的经纬度,得到一个距离最近的城市.详细的和大家描述一下.
比如点 深圳(x1,y1)–>(地点名称)(纬度,经度)
北京(x2,y2)–>(地点名称)(纬度,经度)
如果我现在的定位地点是广州(x3,y3).
那么广州和深圳的距离是[(x3-x1)(x3-x1)]开方,同理广州和北京的距离是[(x3-x2)(x3-x2)]开方.比较两段距离的长度,判断自己离哪个城市更近,则选择哪个城市.
需求就是这么简单.
一下是源码和包
<MainActivity>
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
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;
public class MainActivity extends Activity {
private GridView gridview;
private List<Map<String, String>>list = new ArrayList<Map<String,String>>();
private TextView city_tv;
private String back_city;
private List<Map<String, String>>list_hz = new ArrayList<Map<String,String>>();
/******************************************定位代码的实现*************************/
private String TAG = "MainActivity";
/** 定位成功标识 */
private final int LOCATION_SUCCESS = 1;
/** 定位失败标识 */
private final int LOCATION_ERROR = 0;
/** 百度定位器 */
private LocationClient mLocClient;
/** 延迟发送,设置成全局,用于解决快速按按钮导致的开启了多个线程 */
private Runnable mRunable;
/******************************************定位代码的实现*************************/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
back_city = "深圳";
/******************************************定位代码的实现*************************/
initData();
/******************************************定位代码的实现*************************/
//初始化id
setViews();
//设置城市列表属性
setGridView();
//设置国家城市经纬度
setHZ();
/******************************************定位代码的实现*************************/
mRunable = new Runnable() {
@Override
public void run() {
mLocClient.start();// 开始定位
}
};
handler.postDelayed(mRunable,0);
/******************************************定位代码的实现*************************/
city_tv.setText("正在定位...");
}
//初始化城市坐标点数据
private void setHZ() {
list_hz = new ArrayList<Map<String,String>>();
String str[] = {
"北京","上海","广州",
"深圳","杭州","天津",
"苏州","南京","成都",
"重庆","西安","武汉",
"沈阳","宁波","长沙",
"济南","郑州","无锡",
"青岛","哈尔滨","温州",
"福州","香港"};
String xx[] = {
"39.26","30.4","23.11",
"22.37","30.3","38.33",
"31.3","32.03","30.67",
"29.5","34.15","29.58",
"41.8","28.51","27.51",
"36.40","34.16","31.7",
"35.35","44.04","27.03",
"26.08","22.15"};
String yy[] = {
"115.25","120.51","113.27",
"114.04","120.2","116.42",
"120.6","118.46","104.06",
"106.5","108.56","113.41",
"123.4","120.55","111.53",
"117","112.42","119.33",
"119.30","125.42","119.37",
"119.28","114.15"};
for (int i = 0; i < str.length; i++) {
Map<String, String>map = new HashMap<String, String>();
map.put("address", str[i]);
map.put("weidu", xx[i]);
map.put("jingdu", yy[i]);
list_hz.add(map);
}
}
/******************************************定位代码的实现*************************/
private void initData() {
Log.i("TGA", "===initData()");
/* 初始化定位信息 */
mLocClient = new LocationClient(this);// 创建定位器
// mLocClient.setAK("64qAcRkfBfe6Rh6c37tfEAi8");// 设置Key值
mLocClient.registerLocationListener(mLocationListener);
LocationClientOption mLocOption =
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。