1

I am currently trying to build an app that will launch the maps app to the current GPS location. The user would then be able to place a marker at that location. I have a layout with a simple Place Marker button that correctly launches the map app, but I'm not sure how to get it to zone in on the user's current GPS location. Right now I have it hard-coded to go to a certain address.

Button placeMarkerButton = (Button) findViewById(R.id.bLaunchMaps); 
        placeMarkerButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    String address = " 1120 N Street Sacramento, Sacramento CA 94273";
                    Intent maps = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + address));
                    startActivity(maps);
                } catch (Exception e) {
                    Log.e(TAG, e.toString());
                }
            }
        });

Can anyone provide insight on how I might achieve this? A working example would be great since I am still fairly new to Android development.

Jack
  • 131
  • 1
  • 3
  • 13

2 Answers2

1

this is my GPS class

public class GPS {

    private static final TAG = "GPS";
    private static boolean pGps, pNetwork;
    private static LocationManager locManager;
    private static String provider;
    private static double longitude;
    private static double latitude;


    private static void updateAvailability(){
        try {
            pNetwork = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            provider = LocationManager.NETWORK_PROVIDER;
        } catch (Exception ex) {
            Log.w(TAG,"Ex getting NETWORK provider");
        }
        try {
            pGps = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            provider = LocationManager.GPS_PROVIDER;
        } catch (Exception ex) {
            Log.w(TAG,"Ex getting GPS provider");
        }
    }

    public static Location getLastLocation(Context ctx){
        Location loc = null;
        if(ctx != null){
            if(locManager == null){
                locManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
            }
            updateAvailability();
            if(provider!=null){
                loc = locManager.getLastKnownLocation(provider);
            }
        }
        return loc;
    }       


}

Now from you button you can show the map with the right latitude and logitude.

Button placeMarkerButton = (Button) findViewById(R.id.bLaunchMaps); 
placeMarkerButton.setOnClickListener(new OnClickListener() {     
  @Override
  public void onClick(View v) {
  try {  
  Location loc = GPS.getLastLocation(this.getApplicationContext());
  Double myLongitude = loc.getLongitude();
  Double myLatitude = loc.getLatitude();
  //using google maps you will create a map setting lat & long.     
  String urlAddress = "http://maps.google.com/maps?q="+ myLatitude +"," + myLongitude +"("+ title + ")&iwloc=A&hl=es";     
  //second option:: urlAddress = "http://maps.googleapis.com/maps/api/streetview?size=500x500&location=" + myLatitude + "," + myLongitude + "&fov=90&heading=235&pitch=10&sensor=false";
  //third option:: urlAddress = "geo:<" + myLatitude + ">,<" + myLongitude +">?q=<" + latitude + ">,<" + longitude +">(this is my currently location)"                                
  Intent maps = new Intent(Intent.ACTION_VIEW, Uri.parse(urlAddress)));
  startActivity(maps);
  } catch (Exception e) {
  Log.e(TAG, e.toString());
  }
 }
});
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 1
    Thank you very much @Elenasys. This works great, I just had to change the `latitude` and `longitude` in your `String urlAddress` to `myLongitude` and `myLatitude` respectively. – Jack Feb 04 '14 at 07:34
  • I'm having issues with this sometimes causing the location to not be found when using an older version of Maps. Our QA team reported it on Maps version `6.12.0`. Trying to figure out why this is. – theblang Jul 30 '15 at 15:13
0

Try this:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:<lat>,<long>?q=<lat>,<long>(Label+Name)"));
startActivity(intent);

You can remove (Label+Name) if you don't want a label.

refer to :

How to show marker in Maps launched by geo uri Intent?

Community
  • 1
  • 1
Mohammad Rababah
  • 1,730
  • 4
  • 17
  • 32
  • is anybody else having trouble with labels in geo intents launching properly recently? For me, as of a couple weeks ago, the label broke and isn't displayed in google maps :( – Jacob Beasley Jun 19 '18 at 13:43