如何使用android和java在街道名称上显示位置?
例如:我将输入我的节目街,城市 - 我在地图上获得位置
我有这个样本:
Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:31.06221,33.781642"));
startActivity(i);
如何更改此代码以插入街道和城市?我试试这个:
类似于:Uri.parse("geo:empire state building"));
但它不起作用):
答案 0 :(得分:3)
对Geocoder
类
实现帝国大厦坐标的示例:
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName(
"empire state building", 5);
String add = "";
if (addresses.size() > 0) {
String coords = "geo:" + String.valueOf(addresses.get(0).getLatitude()) + "," + String.valueOf(addresses.get(0).getLongitude());
Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(coords));
startActivity(i);
}
} catch (IOException e) {
e.printStackTrace();
}
答案 1 :(得分:0)
Geocoder很棒,谢谢llya。
这个查询应该在一个单独的线程中执行,例如,AsyncTask,这里有一个示例代码,希望对你有帮助。
public class AsyncTaskToQueryLocation extends
AsyncTask<String, Integer, LatLng> {
private WeakReference<Context> m_Context;
private WeakReference<UserLocationManager> m_Manager;
private WeakReference<OnGeoLocationQueryListener> m_Listener;
private Locale m_Locale;
private String m_ParsedLocation;
public AsyncTaskToQueryLocation(Context context,
UserLocationManager manager, OnGeoLocationQueryListener listener,
Locale locale) {
m_Context = new WeakReference<Context>(context);
m_Manager = new WeakReference<UserLocationManager>(manager);
m_Listener = new WeakReference<OnGeoLocationQueryListener>(listener);
m_Locale = locale;
m_ParsedLocation = null;
}
@Override
protected LatLng doInBackground(String... params) {
Context context = m_Context.get();
if (context == null) {
return null;
}
if ((params == null) || (params.length == 0) || (params[0] == null)) {
return null;
}
m_ParsedLocation = params[0];
Geocoder geoCoder = new Geocoder(context, m_Locale);
try {
List<Address> addresses = geoCoder.getFromLocationName(
m_ParsedLocation, 1);
if (addresses.size() > 0) {
return new LatLng(addresses.get(0).getLatitude(), addresses
.get(0).getLongitude());
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(LatLng latLng) {
OnGeoLocationQueryListener listener = m_Listener.get();
if (listener != null) {
listener.onGeoLocationQueryFinished(m_ParsedLocation, latLng);
}
onCancelled();
}
@Override
protected void onCancelled() {
UserLocationManager manager = m_Manager.get();
if (manager != null) {
manager.notifyAsyncTaskFinish(AsyncTaskToQueryLocation.class
.getSimpleName());
}
clearReference();
}
private void clearReference() {
// release the weak reference.
m_Listener.clear();
m_Manager.clear();
m_Context.clear();
}
}