#Google Map #Google MapView
Full Google Map Introduction
(https://www.raywenderlich.com/144066/introduction-google-maps-api-android)
Mapview Tutorial
(https://www.bignerdranch.com/blog/embedding-custom-views-with-mapview-v2/)
The template adds the following in the manifests/AndroidManifest.xml:
1.<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
A declaration of the ACCESS_FINE_LOCATION permission. This is required to access the user’s precise location.
2.The com.google.android.geo.API_KEY meta-data. This is used to specify the API key.
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
3.The template also adds a Google Play Services dependency to build.gradle.
This dependency exposes the Google Maps and Location Services APIs to the application.
compile 'com.google.android.gms:play-services:VERSION_HERE'
// 1
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
// 2
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
// 3
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
Head back to google_maps_api.xml, replace the value of google_maps_key key with the copied API key.
Build and run again. You should see a map with a red marker on the screen.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, GoogleMap.OnMarkerClickListener, LocationListener
Here’s what each interface does:
1.GoogleApiClient.ConnectionCallbacks provides callbacks that are triggered when the client is connected (onConnected()) or temporarily disconnected (onConnectionSuspended()) from the service.
2.GoogleApiClient.OnConnectionFailedListener provides a callback method (onConnectionFailed()) that is triggered when an attempt to connect the client to the service results in a failure.
3.GoogleMap.OnMarkerClickListener defines the onMarkerClick() which is called when a marker is clicked or tapped.
4.LocationListener defines the onLocationChanged() which is called when a user’s location changes. This method is only called if the LocationListener has been registered.
private GoogleApiClient mGoogleApiClient;
onCreate()
// 1
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
Method()
@Override
protected void onStart() {
super.onStart();
// 2
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
// 3
if( mGoogleApiClient != null && mGoogleApiClient.isConnected() ) {
mGoogleApiClient.disconnect();
}
}
Here’s what’s going on above:
1.Instantiates the mGoogleApiClient field if it’s null.
2.Initiates a background connection of the client to Google Play services.
3.Closes the connection to Google Play services if the client is not null and is connected.
onMapReady()
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setOnMarkerClickListener(this);
setTitle
LatLng myPlace = new LatLng(40.73, -73.99); // this is New York
mMap.addMarker(new MarkerOptions().position(myPlace).title("My Favorite City"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(myPlace));
moveCamera()
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myPlace, 12));
Starting with Android 6.0, user permissions are handled a little differently than before.
You don’t request permission during the installation of your app;
rather, you request them at run time when the permission is actually required.
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
setUpMap()
private void setUpMap() {
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
return;
}
}
onConnected()
@Override
public void onConnected(@Nullable Bundle bundle) {
setUpMap();
}