当前位置: 编程技术>移动开发
本页文章导读:
▪通过Location获取Address的使用 通过Location获取Address的使用!
大家好,上一节我讲了一下如何通过LocationManager来获取Location,没有看过上一节的同学,可以点击如下链接返回查看:Android高手进阶教程十四之---Android Locatio.........
▪ 自定义ViewGroup 实现拖动和快速滚动的效果 自定义ViewGroup 实现拖动跟快速滚动的效果
之前做到个项目要类似listView或者GridView中的控件移动的效果(主屏上所有程序列表上的效果):1:子控件跟着手指移动2:快速拨动一下,根.........
▪ listview 推迟加载图片 listview 延迟加载图片
listview 延迟加载图片该连接未使用软指针http://stackoverflow.com/questions/1409623/android-issue-with-lazy-loading-images-into-a-listview对第一个连接的问题进行探讨http://stackoverflow.com/ques.........
[1]通过Location获取Address的使用
来源: 互联网 发布时间: 2014-02-18
通过Location获取Address的使用!
大家好,上一节我讲了一下如何通过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>
大家好,上一节我讲了一下如何通过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>
[2] 自定义ViewGroup 实现拖动和快速滚动的效果
来源: 互联网 发布时间: 2014-02-18
自定义ViewGroup 实现拖动跟快速滚动的效果
之前做到个项目要类似listView或者GridView中的控件移动的效果(主屏上所有程序列表上的效果):
1:子控件跟着手指移动
2:快速拨动一下,根据拨动的速度 滑动过去
3:拖过头,放手后弹回去
但是用listView或者GridView又不好实现项目要求的其他效果..于是继承viewGroup实现以上效果。
既然要获取拨动速度,并以此滑动。首先想到了OnGestureListener 这个接口,实现这个接口并实现其onFling方法.
还要控制拖动。重写onTouchEvent方法,并在其中控制内容控件的拖动,反弹等效果
这时候基本已经完成了。。。。测试了一下了,发现了一个问题,当手指点在viewGroup上
进行 拖动是没问题的,但是在子控件上就不行了,这是事件响应的问题 那么还要做如面的处
理:实现onInterceptTouchEvent方法,判断是拖动事件时 ,将事件传递下去。
好了,代码就不在上面贴了,如果有需要可以下载附件。 多谢阅读
之前做到个项目要类似listView或者GridView中的控件移动的效果(主屏上所有程序列表上的效果):
1:子控件跟着手指移动
2:快速拨动一下,根据拨动的速度 滑动过去
3:拖过头,放手后弹回去
但是用listView或者GridView又不好实现项目要求的其他效果..于是继承viewGroup实现以上效果。
既然要获取拨动速度,并以此滑动。首先想到了OnGestureListener 这个接口,实现这个接口并实现其onFling方法.
还要控制拖动。重写onTouchEvent方法,并在其中控制内容控件的拖动,反弹等效果
这时候基本已经完成了。。。。测试了一下了,发现了一个问题,当手指点在viewGroup上
进行 拖动是没问题的,但是在子控件上就不行了,这是事件响应的问题 那么还要做如面的处
理:实现onInterceptTouchEvent方法,判断是拖动事件时 ,将事件传递下去。
好了,代码就不在上面贴了,如果有需要可以下载附件。 多谢阅读
1 楼
zhanhao
2012-03-01
赞一个
2 楼
hack_zhang
2012-06-18
谢谢分享 现在正在研究这个
3 楼
hack_zhang
2012-06-18
最好是左右拖动的
[3] listview 推迟加载图片
来源: 互联网 发布时间: 2014-02-18
listview 延迟加载图片
listview 延迟加载图片
该连接未使用软指针
http://stackoverflow.com/questions/1409623/android-issue-with-lazy-loading-images-into-a-listview
对第一个连接的问题进行探讨
http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview
不错的例子
http://code.google.com/p/shelves/
listview 延迟加载图片
该连接未使用软指针
http://stackoverflow.com/questions/1409623/android-issue-with-lazy-loading-images-into-a-listview
对第一个连接的问题进行探讨
http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview
不错的例子
http://code.google.com/p/shelves/
最新技术文章: