Friday, 12 December 2014

Google Map with Multiple Marks Using For loop And Click Event On Marks

XML



<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:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</RelativeLayout>



Java Code


public class MainActivity extends FragmentActivity {

GoogleMap googleMap;
double locationCount = 1.32656;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

SupportMapFragment fm=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
googleMap=fm.getMap();

LatLng latelong = null;

for (int i = 0; i < 10; i++)
{

double lat = 47.612194+(locationCount*i);
double log = -122.344831+(locationCount*i);
latelong=new LatLng(lat, log);

googleMap.addMarker(new MarkerOptions().position(latelong).title("2121 1st Ave, Suite 101 Seattle, WA 98121"));

if(i==0)
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latelong, 15));

}


googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {

@Override
public boolean onMarkerClick(Marker arg0) {
// TODO Auto-generated method stub


Toast.makeText(MainActivity.this, "Pankaj Saad", Toast.LENGTH_SHORT).show();

//when pass true in return title on marks is not show
return false;
}
});



}
}


Manifast XML


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mapwithmultipalmarks"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

 
     <uses-permission android:name="android.permission.INTERNET"/>
   
   
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <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.ACCESS_NETWORK_STATE"/>

 
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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="AIzaSyAEmq9Ag26cjIe_pYybYrRWSNz8scOAd64" />
    </application>


</manifest>

1 comment:

  1. Very useful code, please do update with more tutorials

    ReplyDelete