我有另一个活动的地址列表(不是Lat,Lng)。现在我想通过Intent extras将这些地址传递给我的MapActivity并对其进行地理编码。我怎样才能做到这一点?
以下是我的Listview活动中的地址数据
{{1}}
答案 0 :(得分:1)
Pass your data to activity using list or map and use following code to get the lat long from address
public void getLocationFromAddress(String strAddress){
Geocoder coder = new Geocoder(this);
List<Address> address;
GeoPoint p1 = null;
try {
address = coder.getFromLocationName(strAddress,5);
if (address==null) {
return null;
}
Address location=address.get(0);
location.getLatitude();
location.getLongitude();
Log.i("TAG","Lat = "+location.getLatitude())
Log.i("TAG","Long = "+location.getLongitude())
}
}
Also Check This Geocoding Example
答案 1 :(得分:0)
public class GeoData {
private String address,lat,lng;
public GeoData() {
this("", "0", "0");
}
public GeoData(String address, String lat, String lng) {
this.address = address;
this.lat = lat;
this.lng = lng;
}
public String getAddress() {
return address;
}
public String getLat() {
return lat;
}
public String getLng() {
return lng;
}
public void setAddress(String address) {
this.address = address;
}
public void setLat(String lat) {
this.lat = lat;
}
public void setLng(String lng) {
this.lng = lng;
}
}
在您的活动中创建以下方法。
private GeoData getAddresesFronJson(JSONObject jsonObj) {
if (jsonObj == null) {
return null;
}
try {
// Create a JSON object hierarchy from the results
JSONArray resultJsonArray = jsonObj.getJSONArray("results");
// Extract the Place descriptions from the results
ArrayList<GeoData> resultList = new ArrayList<GeoData>(
resultJsonArray.length());
JSONObject locationObject = null;
for (int i = 0; i < resultJsonArray.length(); i++) {
GeoData data = new GeoData();
data.setAddress(resultJsonArray.getJSONObject(i).getString(
"formatted_address"));
locationObject = resultJsonArray.getJSONObject(i)
.getJSONObject("geometry").getJSONObject("location");
data.setLat(locationObject.getString("lat"));
data.setLng(locationObject.getString("lng"));
return data;
}
} catch (JSONException e) {
Log.e("JSONObject", "Cannot process JSON results", e);
}
return null;
}
private void computeAddress(String address) {
address = address.replaceAll(" ", "%20");
String apiKey = "YOUR PROJECT API KEY(Genarate from google console))";
String url = "https://maps.googleapis.com/maps/api/geocode/json?address="
+ address + "&key=" + apiKey;
// Log.i("googleapis--url:", url.toString());
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.GET, url, null,
new com.android.volley.Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
GeoData geoData = getAddresesFronJson(jsonObject);
if (geoData == null) {
return;
}
// Use this geoData object
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
}
只需使用computeAddress(String address)方法获取Geocode对象。 通过使用地理编码,您可以在地图中显示地址标记。