使用android的定位服务,需要在manifest文件里增加相应的权限,这里不赘述。
下面是两个类的代码,第一个是activity,完成的功能是获取经纬度,然后提供查询对应的地址的按钮。
第二个是工具类,从经纬度获取到地址。
蛋疼的是,不知道为什么,我都那样写了,获取到的数据还是英文的,而从浏览器上获取到的却是中文的。求解。
下面是获取到的json数据的格式:
纠结了一个小时,在网上转了一大圈之后,才发现应该这样子写:“http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&language=zh-CN&sensor=true”。。真的是估计只有经常使用它的人,和它的开发人员,才知道。。貌似文档中也没有提供相关说明。。(
下面是两个类的代码,第一个是activity,完成的功能是获取经纬度,然后提供查询对应的地址的按钮。
第二个是工具类,从经纬度获取到地址。
- /*
- * @(#)LocationActivity.java Project:androidDevices
- * Date:2012-6-5
- *
- * Copyright (c) 2011 CFuture09, Institute of Software,
- * Guangdong Ocean University, Zhanjiang, GuangDong, China.
- * All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.sinaapp.msdxblog.android.location;
- import android.app.Activity;
- import android.content.Context;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Toast;
- import com.sinaapp.msdxblog.android.R;
- /**
- * @author Geek_Soledad (66704238@51uc.com)
- */
- public class LocationActivity extends Activity {
- private double mLongitude;
- private double mLatitude;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.location);
- }
- public void onButtonClick(View v) {
- switch (v.getId()) {
- case R.id.get_location:
- getLocation();
- break;
- case R.id.query:
- Toast.makeText(this,
- LocationUtil.getAddress(mLatitude, mLongitude), 1000)
- .show();
- break;
- default:
- break;
- }
- }
- private void getLocation() {
- LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- locationManager.requestLocationUpdates(
- LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
- @Override
- public void onStatusChanged(String provider, int status,
- Bundle extras) {
- }
- @Override
- public void onProviderEnabled(String provider) {
- Toast.makeText(LocationActivity.this, "enable", 1000)
- .show();
- System.out.println("enable");
- }
- @Override
- public void onProviderDisabled(String provider) {
- Toast.makeText(LocationActivity.this, "disable", 1000)
- .show();
- System.out.println("disable");
- }
- @Override
- public void onLocationChanged(Location location) {
- mLatitude = location.getLatitude();
- mLongitude = location.getLongitude();
- System.out.println(location.getLatitude());
- System.out.println(location.getLongitude());
- Toast.makeText(LocationActivity.this,
- mLatitude + ":" + mLongitude, 1000).show();
- }
- });
- }
- }
- /*
- * @(#)LocationUtil.java Project:androidDevices
- * Date:2012-6-5
- *
- * Copyright (c) 2011 CFuture09, Institute of Software,
- * Guangdong Ocean University, Zhanjiang, GuangDong, China.
- * All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.sinaapp.msdxblog.android.location;
- import java.nio.charset.Charset;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.json.JSONTokener;
- import com.sinaapp.msdxblog.androidkit.net.DownloadUtil;
- /**
- * @author Geek_Soledad (66704238@51uc.com)
- */
- public class LocationUtil {
- public static String getAddress(double lat, double lng) {
- String url = String
- .format("http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&language=CN&sensor=true",
- lat, lng);
- System.out.println(url);
- String result = DownloadUtil.downloadByUrl(url,
- Charset.defaultCharset());
- return jsonSax(result);
- }
- private static String jsonSax(String in) {
- String address = null;
- try {
- JSONTokener tokener = new JSONTokener(in);
- JSONObject results = (JSONObject) tokener.nextValue();
- if (!results.getString("status").equals("OK")) {
- return "无法获取具体地址。";
- }
- System.out.println(results.toString());
- JSONObject result = (JSONObject) results.getJSONArray("results")
- .get(0);
- address = result.getString("formatted_address");
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return address;
- }
- }
蛋疼的是,不知道为什么,我都那样写了,获取到的数据还是英文的,而从浏览器上获取到的却是中文的。求解。
下面是获取到的json数据的格式:
- {
- "results" : [
- {
- "address_components" : [
- {
- "long_name" : "外环西路",
- "short_name" : "外环西路",
- "types" : [ "route" ]
- },
- {
- "long_name" : "麻章区",
- "short_name" : "麻章区",
- "types" : [ "sublocality", "political" ]
- },
- {
- "long_name" : "湛江",
- "short_name" : "湛江",
- "types" : [ "locality", "political" ]
- },
- {
- "long_name" : "广东省",
- "short_name" : "广东省",
- "types" : [ "administrative_area_level_1", "political" ]
- },
- {
- "long_name" : "中国",
- "short_name" : "CN",
- "types" : [ "country", "political" ]
- }
- ],
- "formatted_address" : "中国广东省湛江市麻章区外环西路",
- "geometry" : {
- "bounds" : {
- "northeast" : {
- "lat" : 21.15437220,
- "lng" : 110.29786920
- },
- "southwest" : {
- "lat" : 21.15323980,
- "lng" : 110.29655030
- }
- },
- "location" : {
- "lat" : 21.15385030,
- "lng" : 110.29716670
- },
- "location_type" : "APPROXIMATE",
- "viewport" : {
- "northeast" : {
- "lat" : 21.15515498029150,
- "lng" : 110.2985587302915
- },
- "southwest" : {
- "lat" : 21.15245701970850,
- "lng" : 110.2958607697085
- }
- }
- },
- "types" : [ "route" ]
- },
- {
- "address_components" : [
- {
- "long_name" : "麻章区",
- "short_name" : "麻章区",
- "types" : [ "sublocality", "political" ]
- },
- {
- "long_name" : "湛江",
- "short_name" : "湛江",
- "types" : [ "locality", "political" ]
- },
- {
- "long_name" : "广东省",
- "short_name" : "广东省",
- "types" : [ "administrative_area_level_1", "political" ]
- },
- {
- "long_name" : "中国",
- "short_name" : "CN",
- "types" : [ "country", "political" ]
- }
- ],
- "formatted_address" : "中国广东省湛江市麻章区",
- "geometry" : {
- "bounds" : {
- "northeast" : {
- "lat" : 21.32472990,
- "lng" : 110.64709280
- },
- "southwest" : {
- "lat" : 20.85674290,
- "lng" : 110.12168620
- }
- },
- "location" : {
- "lat" : 21.2633380,
- "lng" : 110.33420
- },
- "location_type" : "APPROXIMATE",
- "viewport" : {
- "northeast" : {
- "lat" : 21.32472990,
- "lng" : 110.64709280
- },
- "southwest" : {
- "lat" : 20.85674290,
- "lng" : 110.12168620
- }
- }
- },
- "types" : [ "sublocality", "political" ]
- },
- {
- "address_components" : [
- {
- "long_name" : "湛江",
- "short_name" : "湛江",
- "types" : [ "locality", "political" ]
- },
- {
- "long_name" : "广东省",
- "short_name" : "广东省",
- "types" : [ "administrative_area_level_1", "political" ]
- },
- {
- "long_name" : "中国",
- "short_name" : "CN",
- "types" : [ "country", "political" ]
- }
- ],
- "formatted_address" : "中国广东省湛江市",
- "geometry" : {
- "bounds" : {
- "northeast" : {
- "lat" : 21.95539610,
- "lng" : 110.97175080
- },
- "southwest" : {
- "lat" : 20.2210810,
- "lng" : 109.66829810
- }
- },
- "location" : {
- "lat" : 21.2707020,
- "lng" : 110.3593870
- },
- "location_type" : "APPROXIMATE",
- "viewport" : {
- "northeast" : {
- "lat" : 21.40704480,
- "lng" : 110.5320740
- },
- "southwest" : {
- "lat" : 21.09339670,
- "lng" : 110.20797730
- }
- }
- },
- "types" : [ "locality", "political" ]
- },
- {
- "address_components" : [
- {
- "long_name" : "广东省",
- "short_name" : "广东省",
- "types" : [ "administrative_area_level_1", "political" ]
- },
- {
- "long_name" : "中国",
- "short_name" : "CN",
- "types" : [ "country", "political" ]
- }
- ],
- "formatted_address" : "中国广东省",
- "geometry" : {
- "bounds" : {
- "northeast" : {
- "lat" : 25.51677140,
- "lng" : 117.31808370
- },
- "southwest" : {
- "lat" : 20.2210810,
- "lng" : 109.66829810
- }
- },
- "location" : {
- "lat" : 23.1321910,
- "lng" : 113.2665310
- },
- "location_type" : "APPROXIMATE",
- "viewport" : {
- "northeast" : {
- "lat" : 25.51677140,
- "lng" : 117.31808370
- },
- "southwest" : {
- "lat" : 20.2210810,
- "lng" : 109.66829810
- }
- }
- },
- "types" : [ "administrative_area_level_1", "political" ]
- },
- {
- "address_components" : [
- {
- "long_name" : "中国",
- "short_name" : "CN",
- "types" : [ "country", "political" ]
- }
- ],
- "formatted_address" : "中国",
- "geometry" : {
- "bounds" : {
- "northeast" : {
- "lat" : 53.56097399999999,
- "lng" : 134.772810
- },
- "southwest" : {
- "lat" : 18.15352160,
- "lng" : 73.49941369999999
- }
- },
- "location" : {
- "lat" : 35.861660,
- "lng" : 104.1953970
- },
- "location_type" : "APPROXIMATE",
- "viewport" : {
- "northeast" : {
- "lat" : 53.56097399999999,
- "lng" : 134.772810
- },
- "southwest" : {
- "lat" : 18.15352160,
- "lng" : 73.49941369999999
- }
- }
- },
- "types" : [ "country", "political" ]
- }
- ],
- "status" : "OK"
- }
纠结了一个小时,在网上转了一大圈之后,才发现应该这样子写:“http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&language=zh-CN&sensor=true”。。真的是估计只有经常使用它的人,和它的开发人员,才知道。。貌似文档中也没有提供相关说明。。(