简体   繁体   中英

Override Default Location on Android Phone using Java

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?

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";

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM