Integrating google maps in android.
Following blog talks about integrating Google Map into your Android applications –
It’s easy to integrate google maps in android app, Infact the only thing you need is an API KEY from google and you can have it in few steps as follows -:
- SignIn to your Google Account and Open google developer console by visting https://console.developers.google.com.
- Create a new Project.
3. Open Project and select Api & Auth section.
4. Inside Api & Auth section select Api, and enable the google map Api.
5. Next move to credentials section and create an android key.
To generate the key You need to have SHA1 fingerprint and package name of your app.To get SHA1 fingerprint run below command on terminal.
keytool -list -v -keystore keystore_path.
But Before that You need to create keystore of your app(so that you can have map on signed apk also).
Using android studio, you can create it by selecting build -> generate singed apk -> create new keystore by giving keystore path,password and alias name.
Put keystore path and run above command and enter keystore password, here you will get SHA1 Key keep that key.
Note : For debugging you can use debug keystore which is created the first time you build your project. By default, it is stored in the same directory as your Android Virtual Device (AVD) files.
6. Now get back to developer console and create android key by putting SHA1 fingerPrint followed by application package name.
Now Make a new android project and create a MapActivity.
Open Manifest File and put the following permissions and generated API KEY.
[sourcecode language=”java”]
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
<Application>
meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="put console Api key here"
</Application>
[/sourcecode]
To show Current Location on your map call requestLocationUpdates() as –
[sourcecode language=”java”]
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("you are here"));
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
mMap.animateCamera(cameraUpdate);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
});
[/sourcecode]
You can also set the type of the MAP. There are four different types of map – Normal,Hybrid,Satellite and terrain and each give different view. You can use them as below.
[sourcecode language=”java”]
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
[/sourcecode]
Enable the zoom gesture in map as.
[sourcecode language=”java”]
googleMap.getUiSettings().setZoomGesturesEnabled(true);
[/sourcecode]
run project to get map.