open google maps through intent for specific locat

2019-01-08 07:40发布

I'm designing one application in which I want to show specific location on Map. I'm passing String of address which is already placed on Google Map. Following is my Intent code..

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

But it gives me Google Map for getting direction. I know why that so, because I used daddr in url but I don't know what to use for specific location..Please tell me what to use there..

8条回答
Juvenile、少年°
2楼-- · 2019-01-08 08:10
  try {
                    Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                            Uri.parse("http://maps.google.com/maps?saddr=" + src_lat+ "," + src_lng + "&daddr=" + des_lat + "," + des_lng));
                    startActivity(intent);
                }catch (ActivityNotFoundException  ane){

                    Toast.makeText(activity, "Please Install Google Maps ", Toast.LENGTH_LONG).show();
                }catch (Exception ex){
                    ex.getMessage();
                }
            }
        });
查看更多
混吃等死
3楼-- · 2019-01-08 08:10

This helper method using uriBuilder for cleaner code and handle condition if there is no activity on device that can open map

public static boolean openMap(Context context, String address) {
    Uri.Builder uriBuilder = new Uri.Builder()
            .scheme("geo")
            .path("0,0")
            .appendQueryParameter("q", address);
    Intent intent = new Intent(Intent.ACTION_VIEW, uriBuilder.build());
    if (intent.resolveActivity(context.getPackageManager()) != null) {
        context.startActivity(intent);
        return true;
    }
    return false;
}
查看更多
登录 后发表回答