How do I show a marker in Maps launched by geo URI Intent?

后端 未结 7 1662
离开以前
离开以前 2020-11-28 02:47

I have a application where I want to show different locations (one at the time, picked by user input) by launching Google Maps with their specific geo coordinates.

I

7条回答
  •  悲&欢浪女
    2020-11-28 03:13

    The accepted answer is correct, except when your label has an ampersand (&) in it.

    Looking at A Uniform Resource Identifier for Geographic Locations ('geo' URI):

    Section 5.1 states:

    if the final URI is to include a 'query' component, add the component delimiter "?" to the end of the result, followed by the encoded query string.

    Unfortunately for us, doing this will also escape the '=' which is not what we want.

    We should do this:

    String label = "Cinnamon & Toast";
    String uriBegin = "geo:12,34";
    String query = "12,34(" + label + ")";
    String encodedQuery = Uri.encode(query);
    String uriString = uriBegin + "?q=" + encodedQuery;
    Uri uri = Uri.parse(uriString);
    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, uri);
    startActivity(intent);
    

提交回复
热议问题