Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First have to obtain the current location:</p> <pre><code>private Location mCurrentLocation; mCurrentLocation = mLocationClient.getLastLocation(); </code></pre> <p>Read <a href="https://developer.android.com/training/location/retrieve-current.html" rel="nofollow">here</a> to know more.</p> <p>And then you can animate to the location using:</p> <pre><code>LatLng myLaLn = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()); CameraPosition camPos = new CameraPosition.Builder().target(myLaLn) .zoom(15) .bearing(45) .tilt(70) .build(); CameraUpdate camUpd3 = CameraUpdateFactory.newCameraPosition(camPos); map.animateCamera(camUpd3); </code></pre> <p>I give you a simple but complete example to show a map and the current location:</p> <pre><code>public class MainActivity extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener { private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; private LocationClient mLocationClient; private Location mCurrentLocation; private GoogleMap map; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map); } @Override protected void onResume() { super.onResume(); setUpMapIfNeeded(); setUpLocationClientIfNeeded(); mLocationClient.connect(); } private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the // map. if (map == null) { // Try to obtain the map from the SupportMapFragment. map = ((SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map)).getMap(); // Check if we were successful in obtaining the map. if (map == null) { Toast.makeText(this, "Google maps not available", Toast.LENGTH_LONG).show(); } } } private void setUpLocationClientIfNeeded() { if (mLocationClient == null) { Toast.makeText(getApplicationContext(), "Waiting for location", Toast.LENGTH_SHORT).show(); mLocationClient = new LocationClient(getApplicationContext(), this, // ConnectionCallbacks this); // OnConnectionFailedListener } } @Override public void onPause() { super.onPause(); if (mLocationClient != null) { mLocationClient.disconnect(); } } /* * Called by Location Services when the request to connect the client * finishes successfully. At this point, you can request the current * location or start periodic updates */ @Override public void onConnected(Bundle dataBundle) { mCurrentLocation = mLocationClient.getLastLocation(); if (mCurrentLocation != null) { Toast.makeText(getApplicationContext(), "Found!", Toast.LENGTH_SHORT).show(); centerInLoc(); } } private void centerInLoc() { LatLng myLaLn = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()); CameraPosition camPos = new CameraPosition.Builder().target(myLaLn) .zoom(15).bearing(45).tilt(70).build(); CameraUpdate camUpd3 = CameraUpdateFactory.newCameraPosition(camPos); map.animateCamera(camUpd3); MarkerOptions markerOpts = new MarkerOptions().position(myLaLn).title( "my Location"); map.addMarker(markerOpts); } /* * Called by Location Services if the connection to the location client * drops because of an error. */ @Override public void onDisconnected() { // Display the connection status Toast.makeText(this, "Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show(); } /* * Called by Location Services if the attempt to Location Services fails. */ @Override public void onConnectionFailed(ConnectionResult connectionResult) { /* * Google Play services can resolve some errors it detects. If the error * has a resolution, try sending an Intent to start a Google Play * services activity that can resolve error. */ if (connectionResult.hasResolution()) { try { // Start an Activity that tries to resolve the error connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); /* * Thrown if Google Play services canceled the original * PendingIntent */ } catch (IntentSender.SendIntentException e) { // Log the error e.printStackTrace(); } } else { /* * If no resolution is available */ Log.e("Home", Integer.toString(connectionResult.getErrorCode())); } } } </code></pre> <p>Note1: I omitted the "Check for Google Play Services" part by simplicity but it should be added as a good practice.</p> <p>Note2: You need the <a href="https://developer.android.com/google/play-services/setup.html#Install" rel="nofollow">google-play-services_lib</a> project and reference it from yours.</p> <p>You can find all information about interacting with google maps in android <a href="https://developers.google.com/maps/documentation/android/interactivity" rel="nofollow">here</a> </p> <p>From the google maps documentation referenced above, just some examples:</p> <p>Zoom controls:</p> <p><em>The Maps API provides built-in zoom controls that appear in the bottom right hand corner of the map. These are enabled by default, but can be disabled by calling</em> <code>UiSettings.setZoomControlsEnabled(boolean)</code>.</p> <p>My Location button:</p> <p><em>The My Location button appears in the top right corner of the screen only when the My Location layer is enabled. When a user clicks the button, the camera animates to focus on the user's current location if the user's location is currently known. A click will also trigger the GoogleMap.OnMyLocationButtonClickListener. You can disable the button from appearing altogether by calling</em> <code>UiSettings.setMyLocationButtonEnabled(boolean)</code>.</p> <p>Add a marker:</p> <p><em>The below example demonstrates how to add a marker to a map. The marker is created at coordinates 0,0, and displays the string "Hello world" in an infowindow when clicked.</em></p> <pre><code>private GoogleMap mMap; mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); mMap.addMarker(new MarkerOptions() .position(new LatLng(0, 0)) .title("Hello world")); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload