所以我搜索了很多关于在谷歌地图中显示一个地方的信息,但是所有的教程都显示了一种需要纬度和经度的技术,但有人可以告诉我一个技巧,我可以使用地址字符串实现这一点吗?谢谢
答案 0 :(得分:0)
我不知道你在android编程方面有多好,但是,有一个名为AutoCompleteTextView的组件,如果你将它的适配器设置为底部代码,它将连接到谷歌地图的地方并搜索你输入到autocompletetextview的地址。
public class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private static final String LOG_TAG = "Test";
private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String OUT_JSON = "/json";
private static final String API_KEY = "Your_Api_Key";
private ArrayList<String> resultList;
public static ArrayList<String> refList=null;
public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public String getItem(int index) {
return resultList.get(index);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
resultList = autocomplete(constraint.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}};
return filter;
}
private ArrayList<String> autocomplete(String input) {
ArrayList<String> resultList = null;
refList=new ArrayList<String>();
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("?sensor=false&key=" + API_KEY);
sb.append("&components=country:tr");
sb.append("&language=tr");
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Error processing Places API URL", e);
return resultList;
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to Places API", e);
return resultList;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
// Extract the Place descriptions from the results
resultList = new ArrayList<String>(predsJsonArray.length());
for (int i = 0; i < predsJsonArray.length(); i++) {
resultList.add(predsJsonArray.getJSONObject(i).getString("description"));
refList.add(predsJsonArray.getJSONObject(i).getString("reference"));
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Cannot process JSON results", e);
}
return resultList;
}
}
答案 1 :(得分:0)
查看Android中的Geocoder。此API包含reverse geocoding
。意味着,您提供了一个地址,如果找到了结果,您将获得Address个对象的ArrayList作为响应。 Address
包含有关该地点的所有必要数据。
你可以像这样使用它:
if(GeoCoder.isPresent()) {
Geocoder geocoder = new Geocoder(this);
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocationName(YOURADDRESS, 5);
if (!addresses.isEmpty()) {
Address address = list.get(0);
// do something with your address
} else {
// No results for your location
}
} catch (IOException e) {
e.printStackTrace();
}
}