通过意向打开地图第一次以用户位置为中心,忽略地理位置



我看到这个例子到处张贴,以打开以特定点为中心的谷歌地图:

String uri = String.format(Locale.ENGLISH, "geo:%f,%f", lat, lon);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);

然而,在我看来,至少在最新的地图版本中,第一次调用它时,它忽略了这一点,而是缩放到用户的位置。这是个虫子吗?

我能够通过AsyncTask两次调用intent来解决这个问题。我不知道这是否是正确的方法,但它对我有效!

这是我的代码:

@Override
public void onClick(View v) {
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            String uri = String.format(Locale.ENGLISH, "geo:0,0?q=%f,%f(%s)&z=40", lat, lon, square);
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
            mContext.startActivity(intent);
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            String uri = String.format(Locale.ENGLISH, "geo:0,0?q=%f,%f(%s)&z=40", lat, lon, square);
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
            mContext.startActivity(intent);
        }
    }.execute();
}

我意识到我从未回答过这个问题。这最终是我使用的,而且似乎还可以。上面的答案也可以!

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
                Uri.parse("http://maps.google.com/maps?q="+lat+","+lon);
startActivity(intent);

要做同样的事情,除了在驾驶模式下启动地图,请执行以下操作:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                Uri.parse("http://maps.google.com/maps?daddr="+lat+","+lon);
startActivity(intent);

相关内容

最新更新