Wednesday, June 3, 2015

Android maximum zoom in Google Map |Android show all markers in Google Map | Android Google Map version 2 with max zoom to show all markers | LatLngBuilder example in Android Google Map

Hi Friends, again I am back with interesting post "How to show all the marker pins in google map in one time?" Are you looking for code to do this? follow given below step-


1)Create new project with blank activity
2)Import Play services lib project inside your project
3)Go to activity_main.xml and create MapFregment like this-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
4)Go to MainActivity.java and paste below code-

package com.androoidhub4you.googlemapmaxzoom;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
/**
* @author manish
*
*/
public class MainActivity extends Activity {
private GoogleMap map;
LatLngBounds.Builder builder;
CameraUpdate cu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**get the reference of map from layout*/
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
/**call the map set up method*/
mSetUpMap();
}
/**create method to set map view*/
public void mSetUpMap() {
/**clear the map before redraw to them*/
map.clear();
/**Create dummy Markers List*/
List<Marker> markersList = new ArrayList<Marker>();
Marker Delhi = map.addMarker(new MarkerOptions().position(new LatLng(
28.61, 77.2099)).title("Delhi"));
Marker Chaandigarh = map.addMarker(new MarkerOptions().position(new LatLng(
30.75, 76.78)).title("Chandigarh"));
Marker SriLanka = map.addMarker(new MarkerOptions().position(new LatLng(
7.000, 81.0000)).title("Sri Lanka"));
Marker America = map.addMarker(new MarkerOptions().position(new LatLng(
38.8833, 77.0167)).title("America"));
Marker Arab = map.addMarker(new MarkerOptions().position(new LatLng(
24.000, 45.000)).title("Arab"));
/**Put all the markers into arraylist*/
markersList.add(Delhi);
markersList.add(SriLanka);
markersList.add(America);
markersList.add(Arab);
markersList.add(Chaandigarh);
/**create for loop for get the latLngbuilder from the marker list*/
builder = new LatLngBounds.Builder();
for (Marker m : markersList) {
builder.include(m.getPosition());
}
/**initialize the padding for map boundary*/
int padding = 50;
/**create the bounds from latlngBuilder to set into map camera*/
LatLngBounds bounds = builder.build();
/**create the camera with bounds and padding to set into map*/
cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
/**call the map call back to know map is loaded or not*/
map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
/**set animated zoom camera into map*/
map.animateCamera(cu);
}
});
}
}
5)Go to your Manifest.xml and make changes and  permissions-

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androoidhub4you.googlemapmaxzoom"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light.NoTitleBar" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDtKRBFRrpjshqoFfCDq-oalPbbgx4XNBs" />
</application>
</manifest>

Download zip code

Thanks,
Manish