Skip to content Skip to sidebar Skip to footer

How To Show My Current Location In Google Map In Android?

In my application I am using Google map API v2. I disabled the My Location Button using map.setMyLocationEnabled(false); and I wanted to add new button to do the same job. How can

Solution 1:

did this myself too, but creating my own header bar and adding a new button. The following code might help (no XML attached, you can sort that yourself). Within your activity add this method:

public GeoPoint where(ArrayList<String> coordinates) {
    double lat = Double.parseDouble((String) coordinates.get(0));
    double lng = Double.parseDouble((String) coordinates.get(1));
    GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
    return p;
}

Then, in the onCreate, add something like this, where you already have locationManager etc. instantiated:

final Button phoneLocButton = (Button) findViewById(R.id.HomeButtonG);
    phoneLocButton.setOnClickListener(new OnClickListener() {
        publicvoidonClick(View v) {
            try {
                locationManager = (LocationManager) getSystemService(context);
                bestProvider = locationManager.getBestProvider(c, true);
                loc = locationManager.getLastKnownLocation(bestProvider);
                if (loc != null) {
                    coords.clear();
                    coords.add("" + loc.getLatitude());
                    coords.add("" + loc.getLongitude());
                }
                mc.animateTo(where(coords));
            } catch (java.lang.IllegalStateException ill) {
                Toast.makeText(getBaseContext(),
                        "Waiting for location fix...", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    });

Hope this helps. I have additional code that centers the map and plonks a pin on it, but this should get you going?

Solution 2:

Post a Comment for "How To Show My Current Location In Google Map In Android?"