open google maps through intent for specific location in android

后端 未结 12 1083
别那么骄傲
别那么骄傲 2020-11-30 20:17

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

相关标签:
12条回答
  • 2020-11-30 20:50

    https://www.google.com/maps/dir//37.4219983,-122.084

    String location = "https://www.google.com/maps/dir//" + latitude + "," + longitude; try { SmsManager sms = SmsManager.getDefault(); // using android SmsManager sms.sendTextMessage(number, null, message + location, null, null); // adding number and text Toast.makeText(getApplicationContext(), "Message Sent", Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(this, "Sms not send, please check phone number/network", Toast.LENGTH_SHORT).show(); e.printStackTrace(); }

    0 讨论(0)
  • 2020-11-30 20:52

    As of 2020 you can also consider using Google Maps URLs, the API designed by Google in order to create universal cross-platform links. You can open Google maps on web, Android or iOS using the same URL string in form:

    https://www.google.com/maps/search/?api=1&parameters

    In case when you need show certain location you can use an URL like

    https://www.google.com/maps/search/?api=1&query=36.26577,-92.54324

    The code snippet is

    String url = "https://www.google.com/maps/search/?api=1&query="+address;
    Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse(url));
    startActivity(intent);
    

    I hope this helps!

    0 讨论(0)
  • 2020-11-30 20:56

    This will work like a charm. Make sure to check for existence for Google Maps, so this can work universally across all non Google devices also. It will open in a browser in such cases.

    Also, remember, don't have www in the URL. Else this will not work.

            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?q=loc:" + latitude + "," + longitude));
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Only if initiating from a Broadcast Receiver
            String mapsPackageName = "com.google.android.apps.maps";
            if (isPackageExisted(context, mapsPackageName)) {
                i.setClassName(mapsPackageName, "com.google.android.maps.MapsActivity");
                i.setPackage(mapsPackageName);
            }
            context.startActivity(i);
    
    private static boolean isPackageExisted(Context context, String targetPackage){
        PackageManager pm=context.getPackageManager();
        try {
            PackageInfo info=pm.getPackageInfo(targetPackage,PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
        return true;
    }
    
    0 讨论(0)
  • 2020-11-30 20:58

    I have not tested this but you could try :

    First method:

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

    EDIT: This might not work with Google maps 7,0

    hence you could change the uri to :

    Second option:

    String geoUri = "http://maps.google.com/maps?q=loc:" + lat + "," + lng + " (" + mTitle + ")";
    

    where mTitle is the name of the location.

    Third option:

    geo:0,0?q=my+street+address
    

    Fourth option:

    String map = "http://maps.google.co.in/maps?q=" + yourAddress;
    

    Hope that works and helps :D..

    0 讨论(0)
  • 2020-11-30 20:59

    The latest version of map provides better solution. If you wish show an address on map use below code.

    Uri mapUri = Uri.parse("geo:0,0?q=" + Uri.encode(address));
    Intent mapIntent = new Intent(Intent.ACTION_VIEW, mapUri);
    mapIntent.setPackage("com.google.android.apps.maps");
    startActivity(mapIntent);
    

    If you want to display using latitude and longitude values, use below method.

    Uri mapUri = Uri.parse("geo:0,0?q=lat,lng(label)");
    Intent mapIntent = new Intent(Intent.ACTION_VIEW, mapUri);
    mapIntent.setPackage("com.google.android.apps.maps");
    startActivity(mapIntent);
    

    Lat and lng being the latitude and longitude you wish to display, the label here is optional.

    0 讨论(0)
  • 2020-11-30 21:02

    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;
    }
    
    0 讨论(0)
提交回复
热议问题