0

我如何使用 android 和 java 按街道名称在地图上显示位置?

例如:我将输入我的程序 street,city - 然后我会在地图上找到位置

我有这个样本:

Intent i = new
        Intent(android.content.Intent.ACTION_VIEW,
        Uri.parse("geo:31.06221,33.781642"));
        startActivity(i);

如何更改此代码以插入街道和城市?我试试这个:

就像是:Uri.parse("geo:empire state building"));

但它不起作用):

4

2 回答 2

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();
        }
于 2012-10-27T14:53:21.933 回答
0

地理编码器很棒,感谢 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();
}

}

于 2014-01-31T20:35:48.923 回答