繁体   English   中英

关于按街道名称在地图上显示位置的咨询

[英]Consultation about show location on map by street name

我如何使用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"));

但它不起作用):

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();
        }

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();
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM