- 浏览: 933982 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
itzhongyuan:
java Random类详解 -
david_je:
你好,我看到你在C里面回调JAVA里面的方法是在native里 ...
Android NDK开发(1)----- Java与C互相调用实例详解 -
fykyx521:
请求锁是在 oncreate 释放实在ondestroy?? ...
Android如何保持程序一直运行 -
aduo_vip:
不错,总结得好!
Android读取assets目录下的资源 -
f839903061:
给的网址很给力哦!
Android 4.0.1 源码下载,编译和运行
大家好,上一节我讲了一下如何通过LocationManager来获取Location,没有看过上一节的同学,可以点击如下链接返回查看:
Android高手进阶教程十四之---Android Location的使用!
我们获取Location的目的之一肯定是有获取这个位置的详细地址,而我们有了Location在来获取Address就相对简单多了,因为GoogleApi已经封装好了方法,我们只需呀通过Location获取GeoPoint,然后在通过GeoPoint来获取我们想要的Address.下面是我做的一个简单的Demo.
第一步新建一个Android工程LocationDemo,注意这里选用的是(Google APIs),下面是文件目录结构
第二步: 修改main.xml(相比第十四节增加了一个address的TextView),代码如下:
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:orientation="vertical"
04. android:layout_width="fill_parent"
05. android:layout_height="fill_parent"
06. >
07.<TextView
08. android:id="@+id/longitude"
09. android:layout_width="fill_parent"
10. android:layout_height="wrap_content"
11. android:text="longitude:"
12. />
13.<TextView
14. android:id="@+id/latitude"
15. android:layout_width="fill_parent"
16. android:layout_height="wrap_content"
17. android:text="latitude:"
18. />
19.<TextView
20. android:id="@+id/address"
21. android:layout_width="fill_parent"
22. android:layout_height="wrap_content"
23. />
24.</LinearLayout>
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:orientation="vertical"
04. android:layout_width="fill_parent"
05. android:layout_height="fill_parent"
06. >
07.<TextView
08. android:id="@+id/longitude"
09. android:layout_width="fill_parent"
10. android:layout_height="wrap_content"
11. android:text="longitude:"
12. />
13.<TextView
14. android:id="@+id/latitude"
15. android:layout_width="fill_parent"
16. android:layout_height="wrap_content"
17. android:text="latitude:"
18. />
19.<TextView
20. android:id="@+id/address"
21. android:layout_width="fill_parent"
22. android:layout_height="wrap_content"
23. />
24.</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="longitude:"
/>
<TextView
android:id="@+id/latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="latitude:"
/>
<TextView
android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
第三步:修改LocationDemo.java(增加了两个方法)代码如下:
view plaincopy to clipboardprint?
01.package com.android.tutor;
02.import java.util.List;
03.import java.util.Locale;
04.import com.google.android.maps.GeoPoint;
05.import android.app.Activity;
06.import android.content.Context;
07.import android.location.Address;
08.import android.location.Geocoder;
09.import android.location.Location;
10.import android.location.LocationManager;
11.import android.os.Bundle;
12.import android.widget.TextView;
13.public class LocationDemo extends Activity {
14.
15. private TextView longitude;
16. private TextView latitude;
17. private TextView address;
18. @Override
19. public void onCreate(Bundle savedInstanceState) {
20. super.onCreate(savedInstanceState);
21. setContentView(R.layout.main);
22.
23. longitude = (TextView)findViewById(R.id.longitude);
24. latitude = (TextView)findViewById(R.id.latitude);
25. address = (TextView)findViewById(R.id.address);
26.
27. Location mLocation = getLocation(this);
28. GeoPoint gp = getGeoByLocation(mLocation);
29. Address mAddress = getAddressbyGeoPoint(this, gp);
30.
31.
32.
33. longitude.setText("Longitude: " + mLocation.getLongitude());
34. latitude.setText("Latitude: " + mLocation.getLatitude());
35. address.setText("Address: " + mAddress.getCountryName()+"," + mAddress.getLocality());
36. }
37.
38. //Get the Location by GPS or WIFI
39. public Location getLocation(Context context) {
40. LocationManager locMan = (LocationManager) context
41. .getSystemService(Context.LOCATION_SERVICE);
42. Location location = locMan
43. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
44. if (location == null) {
45. location = locMan
46. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
47. }
48. return location;
49. }
50. //通过Location获取GeoPoint
51. public GeoPoint getGeoByLocation(Location location) {
52. GeoPoint gp = null;
53. try {
54. if (location != null) {
55. double geoLatitude = location.getLatitude() * 1E6;
56. double geoLongitude = location.getLongitude() * 1E6;
57. gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
58. }
59. } catch (Exception e) {
60. e.printStackTrace();
61. }
62. return gp;
63. }
64. //通过GeoPoint来获取Address
65. public Address getAddressbyGeoPoint(Context cntext, GeoPoint gp) {
66. Address result = null;
67. try {
68. if (gp != null) {
69. Geocoder gc = new Geocoder(cntext, Locale.CHINA);
70.
71. double geoLatitude = (int) gp.getLatitudeE6() / 1E6;
72. double geoLongitude = (int) gp.getLongitudeE6() / 1E6;
73.
74. List<Address> lstAddress = gc.getFromLocation(geoLatitude,
75. geoLongitude, 1);
76. if (lstAddress.size() > 0) {
77. result = lstAddress.get(0);
78. }
79. }
80. } catch (Exception e) {
81. e.printStackTrace();
82. }
83. return result;
84. }
85.}
view plaincopy to clipboardprint?
01.package com.android.tutor;
02.import java.util.List;
03.import java.util.Locale;
04.import com.google.android.maps.GeoPoint;
05.import android.app.Activity;
06.import android.content.Context;
07.import android.location.Address;
08.import android.location.Geocoder;
09.import android.location.Location;
10.import android.location.LocationManager;
11.import android.os.Bundle;
12.import android.widget.TextView;
13.public class LocationDemo extends Activity {
14.
15. private TextView longitude;
16. private TextView latitude;
17. private TextView address;
18. @Override
19. public void onCreate(Bundle savedInstanceState) {
20. super.onCreate(savedInstanceState);
21. setContentView(R.layout.main);
22.
23. longitude = (TextView)findViewById(R.id.longitude);
24. latitude = (TextView)findViewById(R.id.latitude);
25. address = (TextView)findViewById(R.id.address);
26.
27. Location mLocation = getLocation(this);
28. GeoPoint gp = getGeoByLocation(mLocation);
29. Address mAddress = getAddressbyGeoPoint(this, gp);
30.
31.
32.
33. longitude.setText("Longitude: " + mLocation.getLongitude());
34. latitude.setText("Latitude: " + mLocation.getLatitude());
35. address.setText("Address: " + mAddress.getCountryName()+"," + mAddress.getLocality());
36. }
37.
38. //Get the Location by GPS or WIFI
39. public Location getLocation(Context context) {
40. LocationManager locMan = (LocationManager) context
41. .getSystemService(Context.LOCATION_SERVICE);
42. Location location = locMan
43. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
44. if (location == null) {
45. location = locMan
46. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
47. }
48. return location;
49. }
50. //通过Location获取GeoPoint
51. public GeoPoint getGeoByLocation(Location location) {
52. GeoPoint gp = null;
53. try {
54. if (location != null) {
55. double geoLatitude = location.getLatitude() * 1E6;
56. double geoLongitude = location.getLongitude() * 1E6;
57. gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
58. }
59. } catch (Exception e) {
60. e.printStackTrace();
61. }
62. return gp;
63. }
64. //通过GeoPoint来获取Address
65. public Address getAddressbyGeoPoint(Context cntext, GeoPoint gp) {
66. Address result = null;
67. try {
68. if (gp != null) {
69. Geocoder gc = new Geocoder(cntext, Locale.CHINA);
70.
71. double geoLatitude = (int) gp.getLatitudeE6() / 1E6;
72. double geoLongitude = (int) gp.getLongitudeE6() / 1E6;
73.
74. List<Address> lstAddress = gc.getFromLocation(geoLatitude,
75. geoLongitude, 1);
76. if (lstAddress.size() > 0) {
77. result = lstAddress.get(0);
78. }
79. }
80. } catch (Exception e) {
81. e.printStackTrace();
82. }
83. return result;
84. }
85.}
package com.android.tutor;
import java.util.List;
import java.util.Locale;
import com.google.android.maps.GeoPoint;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class LocationDemo extends Activity {
private TextView longitude;
private TextView latitude;
private TextView address;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
longitude = (TextView)findViewById(R.id.longitude);
latitude = (TextView)findViewById(R.id.latitude);
address = (TextView)findViewById(R.id.address);
Location mLocation = getLocation(this);
GeoPoint gp = getGeoByLocation(mLocation);
Address mAddress = getAddressbyGeoPoint(this, gp);
longitude.setText("Longitude: " + mLocation.getLongitude());
latitude.setText("Latitude: " + mLocation.getLatitude());
address.setText("Address: " + mAddress.getCountryName()+"," + mAddress.getLocality());
}
//Get the Location by GPS or WIFI
public Location getLocation(Context context) {
LocationManager locMan = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Location location = locMan
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null) {
location = locMan
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
return location;
}
//通过Location获取GeoPoint
public GeoPoint getGeoByLocation(Location location) {
GeoPoint gp = null;
try {
if (location != null) {
double geoLatitude = location.getLatitude() * 1E6;
double geoLongitude = location.getLongitude() * 1E6;
gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
}
} catch (Exception e) {
e.printStackTrace();
}
return gp;
}
//通过GeoPoint来获取Address
public Address getAddressbyGeoPoint(Context cntext, GeoPoint gp) {
Address result = null;
try {
if (gp != null) {
Geocoder gc = new Geocoder(cntext, Locale.CHINA);
double geoLatitude = (int) gp.getLatitudeE6() / 1E6;
double geoLongitude = (int) gp.getLongitudeE6() / 1E6;
List<Address> lstAddress = gc.getFromLocation(geoLatitude,
geoLongitude, 1);
if (lstAddress.size() > 0) {
result = lstAddress.get(0);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
第四步:最重要一步在AndroidManiefest.xml中导入Google Api(第14行代码)库,代码如下:
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<manifest xmlns:android="http://schemas.android.com/apk/res/android"
03. package="com.android.tutor"
04. android:versionCode="1"
05. android:versionName="1.0">
06. <application android:icon="@drawable/icon" android:label="@string/app_name">
07. <activity android:name=".LocationDemo"
08. android:label="@string/app_name">
09. <intent-filter>
10. <action android:name="android.intent.action.MAIN" />
11. <category android:name="android.intent.category.LAUNCHER" />
12. </intent-filter>
13. </activity>
14. <uses-library android:name="com.google.android.maps" />
15. </application>
16. <uses-sdk android:minSdkVersion="7" />
17. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
18.</manifest>
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<manifest xmlns:android="http://schemas.android.com/apk/res/android"
03. package="com.android.tutor"
04. android:versionCode="1"
05. android:versionName="1.0">
06. <application android:icon="@drawable/icon" android:label="@string/app_name">
07. <activity android:name=".LocationDemo"
08. android:label="@string/app_name">
09. <intent-filter>
10. <action android:name="android.intent.action.MAIN" />
11. <category android:name="android.intent.category.LAUNCHER" />
12. </intent-filter>
13. </activity>
14. <uses-library android:name="com.google.android.maps" />
15. </application>
16. <uses-sdk android:minSdkVersion="7" />
17. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
18.</manifest>
Android高手进阶教程十四之---Android Location的使用!
我们获取Location的目的之一肯定是有获取这个位置的详细地址,而我们有了Location在来获取Address就相对简单多了,因为GoogleApi已经封装好了方法,我们只需呀通过Location获取GeoPoint,然后在通过GeoPoint来获取我们想要的Address.下面是我做的一个简单的Demo.
第一步新建一个Android工程LocationDemo,注意这里选用的是(Google APIs),下面是文件目录结构
第二步: 修改main.xml(相比第十四节增加了一个address的TextView),代码如下:
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:orientation="vertical"
04. android:layout_width="fill_parent"
05. android:layout_height="fill_parent"
06. >
07.<TextView
08. android:id="@+id/longitude"
09. android:layout_width="fill_parent"
10. android:layout_height="wrap_content"
11. android:text="longitude:"
12. />
13.<TextView
14. android:id="@+id/latitude"
15. android:layout_width="fill_parent"
16. android:layout_height="wrap_content"
17. android:text="latitude:"
18. />
19.<TextView
20. android:id="@+id/address"
21. android:layout_width="fill_parent"
22. android:layout_height="wrap_content"
23. />
24.</LinearLayout>
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:orientation="vertical"
04. android:layout_width="fill_parent"
05. android:layout_height="fill_parent"
06. >
07.<TextView
08. android:id="@+id/longitude"
09. android:layout_width="fill_parent"
10. android:layout_height="wrap_content"
11. android:text="longitude:"
12. />
13.<TextView
14. android:id="@+id/latitude"
15. android:layout_width="fill_parent"
16. android:layout_height="wrap_content"
17. android:text="latitude:"
18. />
19.<TextView
20. android:id="@+id/address"
21. android:layout_width="fill_parent"
22. android:layout_height="wrap_content"
23. />
24.</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="longitude:"
/>
<TextView
android:id="@+id/latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="latitude:"
/>
<TextView
android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
第三步:修改LocationDemo.java(增加了两个方法)代码如下:
view plaincopy to clipboardprint?
01.package com.android.tutor;
02.import java.util.List;
03.import java.util.Locale;
04.import com.google.android.maps.GeoPoint;
05.import android.app.Activity;
06.import android.content.Context;
07.import android.location.Address;
08.import android.location.Geocoder;
09.import android.location.Location;
10.import android.location.LocationManager;
11.import android.os.Bundle;
12.import android.widget.TextView;
13.public class LocationDemo extends Activity {
14.
15. private TextView longitude;
16. private TextView latitude;
17. private TextView address;
18. @Override
19. public void onCreate(Bundle savedInstanceState) {
20. super.onCreate(savedInstanceState);
21. setContentView(R.layout.main);
22.
23. longitude = (TextView)findViewById(R.id.longitude);
24. latitude = (TextView)findViewById(R.id.latitude);
25. address = (TextView)findViewById(R.id.address);
26.
27. Location mLocation = getLocation(this);
28. GeoPoint gp = getGeoByLocation(mLocation);
29. Address mAddress = getAddressbyGeoPoint(this, gp);
30.
31.
32.
33. longitude.setText("Longitude: " + mLocation.getLongitude());
34. latitude.setText("Latitude: " + mLocation.getLatitude());
35. address.setText("Address: " + mAddress.getCountryName()+"," + mAddress.getLocality());
36. }
37.
38. //Get the Location by GPS or WIFI
39. public Location getLocation(Context context) {
40. LocationManager locMan = (LocationManager) context
41. .getSystemService(Context.LOCATION_SERVICE);
42. Location location = locMan
43. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
44. if (location == null) {
45. location = locMan
46. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
47. }
48. return location;
49. }
50. //通过Location获取GeoPoint
51. public GeoPoint getGeoByLocation(Location location) {
52. GeoPoint gp = null;
53. try {
54. if (location != null) {
55. double geoLatitude = location.getLatitude() * 1E6;
56. double geoLongitude = location.getLongitude() * 1E6;
57. gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
58. }
59. } catch (Exception e) {
60. e.printStackTrace();
61. }
62. return gp;
63. }
64. //通过GeoPoint来获取Address
65. public Address getAddressbyGeoPoint(Context cntext, GeoPoint gp) {
66. Address result = null;
67. try {
68. if (gp != null) {
69. Geocoder gc = new Geocoder(cntext, Locale.CHINA);
70.
71. double geoLatitude = (int) gp.getLatitudeE6() / 1E6;
72. double geoLongitude = (int) gp.getLongitudeE6() / 1E6;
73.
74. List<Address> lstAddress = gc.getFromLocation(geoLatitude,
75. geoLongitude, 1);
76. if (lstAddress.size() > 0) {
77. result = lstAddress.get(0);
78. }
79. }
80. } catch (Exception e) {
81. e.printStackTrace();
82. }
83. return result;
84. }
85.}
view plaincopy to clipboardprint?
01.package com.android.tutor;
02.import java.util.List;
03.import java.util.Locale;
04.import com.google.android.maps.GeoPoint;
05.import android.app.Activity;
06.import android.content.Context;
07.import android.location.Address;
08.import android.location.Geocoder;
09.import android.location.Location;
10.import android.location.LocationManager;
11.import android.os.Bundle;
12.import android.widget.TextView;
13.public class LocationDemo extends Activity {
14.
15. private TextView longitude;
16. private TextView latitude;
17. private TextView address;
18. @Override
19. public void onCreate(Bundle savedInstanceState) {
20. super.onCreate(savedInstanceState);
21. setContentView(R.layout.main);
22.
23. longitude = (TextView)findViewById(R.id.longitude);
24. latitude = (TextView)findViewById(R.id.latitude);
25. address = (TextView)findViewById(R.id.address);
26.
27. Location mLocation = getLocation(this);
28. GeoPoint gp = getGeoByLocation(mLocation);
29. Address mAddress = getAddressbyGeoPoint(this, gp);
30.
31.
32.
33. longitude.setText("Longitude: " + mLocation.getLongitude());
34. latitude.setText("Latitude: " + mLocation.getLatitude());
35. address.setText("Address: " + mAddress.getCountryName()+"," + mAddress.getLocality());
36. }
37.
38. //Get the Location by GPS or WIFI
39. public Location getLocation(Context context) {
40. LocationManager locMan = (LocationManager) context
41. .getSystemService(Context.LOCATION_SERVICE);
42. Location location = locMan
43. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
44. if (location == null) {
45. location = locMan
46. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
47. }
48. return location;
49. }
50. //通过Location获取GeoPoint
51. public GeoPoint getGeoByLocation(Location location) {
52. GeoPoint gp = null;
53. try {
54. if (location != null) {
55. double geoLatitude = location.getLatitude() * 1E6;
56. double geoLongitude = location.getLongitude() * 1E6;
57. gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
58. }
59. } catch (Exception e) {
60. e.printStackTrace();
61. }
62. return gp;
63. }
64. //通过GeoPoint来获取Address
65. public Address getAddressbyGeoPoint(Context cntext, GeoPoint gp) {
66. Address result = null;
67. try {
68. if (gp != null) {
69. Geocoder gc = new Geocoder(cntext, Locale.CHINA);
70.
71. double geoLatitude = (int) gp.getLatitudeE6() / 1E6;
72. double geoLongitude = (int) gp.getLongitudeE6() / 1E6;
73.
74. List<Address> lstAddress = gc.getFromLocation(geoLatitude,
75. geoLongitude, 1);
76. if (lstAddress.size() > 0) {
77. result = lstAddress.get(0);
78. }
79. }
80. } catch (Exception e) {
81. e.printStackTrace();
82. }
83. return result;
84. }
85.}
package com.android.tutor;
import java.util.List;
import java.util.Locale;
import com.google.android.maps.GeoPoint;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class LocationDemo extends Activity {
private TextView longitude;
private TextView latitude;
private TextView address;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
longitude = (TextView)findViewById(R.id.longitude);
latitude = (TextView)findViewById(R.id.latitude);
address = (TextView)findViewById(R.id.address);
Location mLocation = getLocation(this);
GeoPoint gp = getGeoByLocation(mLocation);
Address mAddress = getAddressbyGeoPoint(this, gp);
longitude.setText("Longitude: " + mLocation.getLongitude());
latitude.setText("Latitude: " + mLocation.getLatitude());
address.setText("Address: " + mAddress.getCountryName()+"," + mAddress.getLocality());
}
//Get the Location by GPS or WIFI
public Location getLocation(Context context) {
LocationManager locMan = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Location location = locMan
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null) {
location = locMan
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
return location;
}
//通过Location获取GeoPoint
public GeoPoint getGeoByLocation(Location location) {
GeoPoint gp = null;
try {
if (location != null) {
double geoLatitude = location.getLatitude() * 1E6;
double geoLongitude = location.getLongitude() * 1E6;
gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
}
} catch (Exception e) {
e.printStackTrace();
}
return gp;
}
//通过GeoPoint来获取Address
public Address getAddressbyGeoPoint(Context cntext, GeoPoint gp) {
Address result = null;
try {
if (gp != null) {
Geocoder gc = new Geocoder(cntext, Locale.CHINA);
double geoLatitude = (int) gp.getLatitudeE6() / 1E6;
double geoLongitude = (int) gp.getLongitudeE6() / 1E6;
List<Address> lstAddress = gc.getFromLocation(geoLatitude,
geoLongitude, 1);
if (lstAddress.size() > 0) {
result = lstAddress.get(0);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
第四步:最重要一步在AndroidManiefest.xml中导入Google Api(第14行代码)库,代码如下:
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<manifest xmlns:android="http://schemas.android.com/apk/res/android"
03. package="com.android.tutor"
04. android:versionCode="1"
05. android:versionName="1.0">
06. <application android:icon="@drawable/icon" android:label="@string/app_name">
07. <activity android:name=".LocationDemo"
08. android:label="@string/app_name">
09. <intent-filter>
10. <action android:name="android.intent.action.MAIN" />
11. <category android:name="android.intent.category.LAUNCHER" />
12. </intent-filter>
13. </activity>
14. <uses-library android:name="com.google.android.maps" />
15. </application>
16. <uses-sdk android:minSdkVersion="7" />
17. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
18.</manifest>
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<manifest xmlns:android="http://schemas.android.com/apk/res/android"
03. package="com.android.tutor"
04. android:versionCode="1"
05. android:versionName="1.0">
06. <application android:icon="@drawable/icon" android:label="@string/app_name">
07. <activity android:name=".LocationDemo"
08. android:label="@string/app_name">
09. <intent-filter>
10. <action android:name="android.intent.action.MAIN" />
11. <category android:name="android.intent.category.LAUNCHER" />
12. </intent-filter>
13. </activity>
14. <uses-library android:name="com.google.android.maps" />
15. </application>
16. <uses-sdk android:minSdkVersion="7" />
17. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
18.</manifest>
相关推荐
Android高手进阶教程之----通过Location获取Address的使用.doc Android基础教程之----Android ProgressBar的使用.doc Android基础教程之----Android中两种设置全屏的方法!!.doc Android基础教程之----Android状态栏...
总结来说,获取Android设备的GPS经纬度并转换为地址,需要利用Location服务获取定位信息,然后通过Geocoding API或`Geocoder`将坐标解析为可读的地址。在实际应用中,还需要考虑位置权限的获取、GPS状态的检查以及...
这样,你就可以在整个应用中通过依赖注入或`App::make('ip.location')`来使用这个服务。 此外,考虑到性能和限制问题,你可能需要缓存获取到的位置信息。Laravel提供了强大的缓存系统,你可以选择合适的方式(如...
首先,获取外网IP地址通常通过HTTP请求到一些公开的IP查询服务,例如IP-api.com或者ip.163.com。在C#中,我们可以使用`HttpClient`类来发送GET请求并处理返回的数据。以下是一个简单的示例: ```csharp using ...
当位置更新时,`onLocationChanged()`方法会被调用,我们可以在这个方法中获取到新的Location对象,包含经纬度信息: ```java Location location = locationEvent.getLocation(); double latitude = location....
Location location = await GetLocationByAddress(address); row.SetField("LatitudeColumn", location.Latitude); row.SetField("LongitudeColumn", location.Longitude); } ``` 对于VS2012及以上版本,这些...
Console.WriteLine($"省份: {location.Address.Region}"); Console.WriteLine($"城市: {location.Address.City}"); Console.WriteLine($"县: {location.Address.District}"); Console.WriteLine($"街道: {location....
对于应用程序开发者而言,获取IP地址主要通过编程接口,例如Android中的`java.net.InetAddress`类。 在Android应用中,获取手机IP地址可以通过以下步骤实现: 1. **权限请求**: 在AndroidManifest.xml中添加`...
以下将详细介绍两种主要的获取方法:GPS(全球定位系统)和网络定位,以及如何通过Geocoder类获取更详细的地理位置信息。 1. GPS定位: GPS是获取精确经纬度的首选方式,它通过接收卫星信号来计算设备的位置。在...
4. **从Location获取城市名**: `Location`对象包含了经纬度信息。可以使用第三方库,如`GeoNames`或Google的`Geocoding API`,将经纬度转换为实际的地址信息,包括城市名: ```java public String ...
5. **获取当前位置**:在LocationListener的`onLocationChanged()`方法中,你可以获取到Location对象,它包含了经纬度信息: ```java double latitude = location.getLatitude(); // 经度 double longitude = ...
这些信息通常封装在`results[0].address_components`中,可以通过`types`属性筛选出特定类型的地址组件,如街道、城市等。 标签中的"jsontoBean"可能意味着在Java后端处理数据时,会用到JSON对象与Java Bean之间的...
- 如果你的应用可以使用C/C++代码,可以通过Android NDK调用Linux系统函数`ifconfig`或`ip link show`来获取Mac地址,但这需要处理更复杂的跨平台问题。 5. **使用SystemProperties获取** - 在系统服务层面,可以...
通过结合Location API和Geocoder,我们不仅可以获得设备的精确位置,还能获取到与该位置相关的详细地址信息。这个过程对于实现导航、定位、位置跟踪等功能至关重要。在实际项目中,还需要考虑到性能优化、权限处理...
这篇博文“通过百度API获取经纬度”提供了关于如何利用百度地图API来实现这一功能的详细指南。百度地图API是一个强大的工具,它允许开发者通过集成到自己的应用程序中,获取到精确的地理位置信息。下面将详细介绍这...
可以创建一个`WeatherService`类,包含`getWeatherByCityName(String cityName)`和`getWeatherByIP(String ipAddress)`方法,分别对应通过城市名和IP地址获取天气。这些方法内部封装了HTTP请求和数据解析的逻辑。...
通过以上步骤,你可以成功地在Android应用中获取GPS的经纬度坐标以及对应的城市信息。不过,需要注意的是,由于GPS定位可能受到信号遮挡等因素影响,可能会出现定位延迟或无法定位的情况。在这种情况下,可以考虑...
百度地图API提供了逆地理编码接口,允许开发者通过输入地址字符串来获取对应的经纬度坐标。 要使用百度地图API进行地址解析,你需要注册一个百度开发者账号并创建一个应用,获取到API密钥(AK)。这个密钥是你在...
本篇文章将详细讲解如何通过百度地图API实现从地名获取经纬度的过程,并探讨如何利用这些经纬度进行定位。 首先,我们要了解经纬度的概念。经纬度是地球上地理位置的一种表示方式,经度(Longitude)是从本初子午线...