Wednesday 19 February 2014

Android Map V2 Marker Example


Hello friends Today I will write a code for Android map v2 marker .

Step 1: Create a project MapV2Example .

Step 2: Import google-play-services_lib in your workspace , this is the path  if you allready installed Google play Service >

sdk\extras\google\google_play_services\libproject   otherwise install google play service using this link >

http://developer.android.com/google/play-services/setup.html

Step 3: Go to Eclipse> Window>Prefrences>Android>Build  Copy SHA1 fingureprint(With heigher sdk) for lower sdk type this command : 

C:\Program Files\Java\jre7\bin>keytool -list -v -keystore "C:\Users\Admin\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android 

you will get SHA1 key from here

Step 4 : Go to api console >Click on Service >Click on to Google Maps Android API v2 

Step 5: Go to Api access Create New Android key 

Step 6 :Insert SHA1 key and package name like this > BC:0D:AC:74:DR:21:E1:41:67:71:9B:62:91:KF:A1:66:6E:44:5D:75;com.ritesh.mapv2example
 (SHA1 Key ,package name) >Click create

Step7 : Now you will get api key from here .

Step 8 : Copy this code in your MainActivity :

package com.ritesh.mapv2example;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends FragmentActivity  {
private GoogleMap map;
private LatLng ltlng;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InitialiseMap();
Button buttonreload1=(Button)findViewById(R.id.buttonreload);
Button buttonreload2=(Button)findViewById(R.id.buttonreload2);
buttonreload1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
map.clear();
ltlng=new LatLng(55, 45);
map.addMarker(new MarkerOptions()
       .position(ltlng)
       .title("Hello world1:"));
}
});
         buttonreload2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
map.clear();
ltlng=new LatLng(75, 65);
map.addMarker(new MarkerOptions()
       .position(ltlng)
       .title("Hello world2:"));
}
});

 }

private void InitialiseMap() {
// TODO Auto-generated method stub
if (map == null) {
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_frag))
                           .getMap();
   
 if (map != null) {
     

     }

 map.setMyLocationEnabled(true);
  for (int i = 60; i < 70; i++) {
  ltlng=new LatLng(i, i+30);

  map.addMarker(new MarkerOptions()
       .position(ltlng)
       .title("Hello world:"+i));
}
}
}
}

Step 9 : Copy this code in activity_main.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_frag"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="50dp" />

    <Button
        android:id="@+id/buttonreload"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Refreshmap1" 
       />
    
    <Button
        android:id="@+id/buttonreload2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Refreshmap2" />

</RelativeLayout>


Step 10 : Copy this code in your manifest file and change your map v2 api key  value (android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyBEBy8jXbOTs6QVoeo3rn6QsiIZ81r52G0"/>)


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ritesh.mapv2example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
    
      <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ritesh.mapv2example.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="4030500" />
        <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyBEBy8jXbOTs6QVoeo3rn6QsiIZ81r52G0"/>
        
    </application>
  

</manifest>

Step 11 : Run the project 



















1 comment: