【问题标题】:how can I get this to display a marker on google maps?我怎样才能让它在谷歌地图上显示一个标记?
【发布时间】:2020-03-25 22:49:48
【问题描述】:

我正在尝试传递从 RSS 提要的点击中收集的坐标和标题,我想将它从调试传递到谷歌地图,它确实通过了没有问题我的问题是在地图上显示它。这是带有意图的onclick:

       public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Intent in = new Intent(getApplicationContext(), MapsActivity.class);
            String georss = ((TextView) view.findViewById(R.id.georss)).getText().toString();
            String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
            String[] latLng = georss.split(" ");
            double lat = Double.parseDouble(latLng[0]);
            double lng = Double.parseDouble(latLng[1]);;
            LatLng location = new LatLng(lat, lng);
            in.putExtra("location", location);
            in.putExtra("title", title);
            startActivity(in);
        }
    });

这是创建时的谷歌地图:

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        Intent intent = getIntent();
        intent.getStringExtra("title");
        intent.getStringExtra("location");

我只是不确定如何显示标记,所以当您单击它时,您可以看到标题。

【问题讨论】:

    标签: java android google-maps


    【解决方案1】:
    intent.getStringExtra("location");
    

    location参数是LatLang,不是String,所以无法从intent中获取位置。 所以最好分开发送lat和lng。

    ...
    in.putExtra("lat", lat);
    in.putExtra("lng", lng);
    startActivity(in);
    
    ...
    Intent intent = getIntent();
    double lat = intent.getDoubleExtra("lat", 0);
    double lng = intent.getDoubleExtra("lng", 0);
    ...
    

    [编辑]

    或者你可以像这样解析 LatLang 数据。

    ...
    in.putExtra("location", location);
    startActivity(in);
    
    ...
    Intent intent = getIntent();
    LatLng location = (LatLng) intent.getExtras().get("location");
    ...
    

    通过这样做,您可以从意图中获取对象数据。但是在这种情况下,您应该检查密钥,否则位置可能为空。 谢谢。

    【讨论】:

      猜你喜欢
      • 2013-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-09
      • 2013-07-22
      • 2021-07-07
      相关资源
      最近更新 更多