您的位置:首页 > 移动开发 > Android开发

android简单定位

2015-07-17 17:11 435 查看
在项目中可能需要在应用启动的时候进行一个大概的定位,然后根据当前定位的信息进行对应的数据显示

这里就用到了定位,下面的例子是一个获取实际位置的示例代码

关键api解释:

1.LocationManager:用于管理Android的用户定位服务

2.LocationProviders:提供多种定位方式供开发者选择。

(一般我们不用直接获取这个对象,直接可以通过LocationManager的getLastKnownLocation方法(传递一个provider的名称就行了)就能获取对应的Location了)

<1>GPS Provider

<2>Network Provider等

3. Location: 用于描述位置信息。

4. Criteria:

用于描述Location Provider标准的类,标准包括位置精度水平,电量消耗水平,是否获取海拔、方位信息,是否允许接收付费服务
(如果我们直接知道要使用的Provider当然就可以不用这个类了)

通过上面的4个类我们就可以获得当前的位置信息了
分析:

1. android定位当然要使用系统中的api了,同时由于我们只要获得一个大概的位置就行了,所以采用network的方式进行取得位置就行了

2. 我们通过android获得的是一个经纬度,所以我们要将经纬度进行转化为具体的地理位置,但是由于android提供的api不能精确的实现,

所以我使用百度的web服务api进行获得具体的地址,当然要使用必须有百度开发者账号。。。。。

具体代码:

<span style="white-space:pre">	</span>    double latitude; //当前位置的纬度
double longitude; //当前位置的经度

private void openGPSSettings(){
LocationManager alm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
doWork(); //根据当前的经纬度获得实际的经纬度
}
private void doWork() {

String locationString; //当前位置信息
//范围百度服务api的地址
String questURL = "http://api.map.baidu.com/geocoder/v2/?output=json";
//获取位置服务的管理器
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 获得当前的位置,这里直接通过network进行获得地址位置
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

latitude = location.getLatitude(); //纬度
longitude = location.getLongitude(); //精度

locationString = "&location=" + latitude + "," + longitude;
String keyString = "&ak=<span style="font-family: Arial, Helvetica, sans-serif;">在百度开发平台上申请的key</span>";
questURL = questURL + locationString + keyString;
Log.i("Location", questURL);
new ReadJSONFeedTask().execute(questURL);
}

/**
* 由经纬度获取所在的城市及区域信息
*/
private class ReadJSONFeedTask extends AsyncTask<String, Void, String> {

StringBuilder stringBuilder = new StringBuilder();

@Override
protected String doInBackground(String... urls) {
return readJSONFeed(urls[0]);
}

@Override
protected void onPostExecute(String result) {
String strItem;
try {
JSONObject jsonObject = new JSONObject(result);
JSONObject resultObject = jsonObject.getJSONObject("result");
JSONObject addressComponentObject = resultObject
.getJSONObject("addressComponent");
String city = addressComponentObject.getString("city");
String district = addressComponentObject.getString("district");

city = "城市:" + city;
district = "    区:" + district;
stringBuilder.append(city + district);
//Toast.makeText(getApplicationContext(), stringBuilder, 3).show();
Log.i("LoctionNew", stringBuilder.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<pre name="code" class="java"><span style="white-space:pre">	</span>  /**
* 请求json数据
* @param url
*/
public String readJSONFeed(String url) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
} else {
Log.e("JSON", "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}



最后加上权限就可以了

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />


百度Web服务API地址:http://developer.baidu.com/map/index.php?title=webapi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: