0

I have followed a 24 hours textbook and created an Android app that is supposed to launch Google maps and navigate to a specific location based on latitude and longitude coordinates hard coded into the app. However, when I launch the app and run the code it opens Google maps or Google Earth and always defaults to my current location. How do I prevent it from doing this?

I am trying to get my app to go to a specific latitude and longitude, but when it launches Google Maps or Google Earth it always defaults to my current location. Java Code is as follows:

Button mapButton = (Button) findViewById(R.id.button2);
mapButton.setOnClickListener(new View.OnClickListener() {
   @override
   public void onClick(View view) {
     String geoURI = "geo:37.422,-122.0847z=23";
     Uri geo = Uri.parse(geoURI);
     Intent mapIntent = new Intent(Intent.ACTION_VIEW, geo);
     if (mapIntent.resolveActivity(getPackageManager()) != null) {
       startActivity(mapIntent);

How do I get it to override the default location and go to the coordinates I have hard coded above?

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Pickles57
  • 13
  • 2
  • 2
    https://stackoverflow.com/questions/3990110/how-to-show-marker-in-maps-launched-by-geo-uri-intent – bhavani Sep 13 '17 at 05:29

3 Answers3

1

Looking at the documentation, the line:

String geoURI = "geo:37.422,-122.0847z=23";

Appears to be invalid syntax, but might be wanting to zoom. Try:

String geoURI = "geo:37.422,-122.0847?z=23";

Zoom level only goes up to 21, however, so also try:

String geoURI = "geo:37.422,-122.0847?z=21";

If neither work, use a basic string without any zoom:

String geoURI = "geo:37.422,-122.0847";
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • Thank you. Yes it was a syntax error. Adding the "?" and changing the zoom level to z=21 fixed the problem – Pickles57 Sep 13 '17 at 13:18
0

Try this:

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

You can omit (Label+Name) if you don't want a label, and it will choose one randomly based on the nearest street or other thing it thinks relevant.

chandrakant sharma
  • 1,334
  • 9
  • 15
0

You can create LatLng object and tell google map to load the location. Code to do that will be something like this:

double lat=26.52719;
double lng=88.72448;
LatLng myLocation = new LatLng(lat,lng);
                if(this.googleMap !=null){
                    this.googleMap.addMarker(new MarkerOptions().position(myLocation).title("Your current Location").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                    this.googleMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation));
                }

Remember you should apply this in onMapReady callback and to turn off your current location placing in google map you can do

this.googleMap.setMyLocationEnabled(true);
Anuran Barman
  • 1,556
  • 2
  • 16
  • 31