Implement google maps in android example with the source code.

copy the following and paste it into your MainActivity.java

package com.example.mapss;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;
import com.google.android.gms.maps.model.PolylineOptions;

public class MainActivity extends Activity {
ArithmeticException area;

GoogleMap map;
ArrayList<LatLng> arrayPoints =null;
PolylineOptions polylineOptions;
PolygonOptions polygonOptions;
RadioGroup rg_views;
int n;

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

arrayPoints = new ArrayList<LatLng>();

map=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.getUiSettings().setZoomControlsEnabled(true);
map.getUiSettings().setCompassEnabled(true);
map.getUiSettings().setMyLocationButtonEnabled(true);

map.setMyLocationEnabled(true);

rg_views =(RadioGroup)findViewById(R.id.rg_views);
rg_views.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub

if (checkedId==R.id.rb_map){
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

}
else if(checkedId==R.id.rb_satellite){
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

}
else if(checkedId==R.id.rb_terrain){
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}

}
});

map.setOnMapClickListener(new OnMapClickListener() {

@Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub

MarkerOptions markerOptions = new MarkerOptions();

markerOptions.position(point);

markerOptions.title(point.latitude + “: ” + point.longitude);

map.clear();

map.animateCamera(CameraUpdateFactory.newLatLng(point));

map.addMarker(markerOptions);

polygonOptions = new PolygonOptions();
polygonOptions.fillColor(Color.RED);
polygonOptions.strokeWidth(5);
arrayPoints.add(point);
polygonOptions.addAll(arrayPoints);
map.addPolygon(polygonOptions);

}
});

map.setOnMapLongClickListener(new OnMapLongClickListener() {

@Override
public void onMapLongClick(LatLng point) {
// TODO Auto-generated method stub

map.clear();
arrayPoints.clear();

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);

open your activity_main and paste the following code.

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.example.mapss.MainActivity” >

<RadioGroup
android:id=”@+id/rg_views”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:gravity=”center_horizontal”
android:orientation=”horizontal”>

<RadioButton
android:id=”@+id/rb_map”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Normal”
android:checked=”true”/>

<RadioButton
android:id=”@+id/rb_satellite”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Satellite”/>

<RadioButton
android:id=”@+id/rb_terrain”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Terrrain”/>

</RadioGroup>

<fragment
android:id=”@+id/map”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_below=”@+id/rg_views”
android:name=”com.google.android.gms.maps.MapFragment”/>

</RelativeLayout>

Now go to the android manifest and then copy the following code.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.mapss”
android:versionCode=”1″
android:versionName=”1.0″ >

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

<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” />
<!–
The following two permissions are not required to use
Google Maps Android API v2, but are recommended.
–>
<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION” />
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />

<permission
android:name=”com.example.mapss.permission.MAPS_RECEIVE”
android:protectionLevel=”signature” />

<uses-permission android:name=”com.example.mapss.permission.MAPS_RECEIVE” />

<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=”enter the key you generated” />
</application>

</manifest>

Remember that you have to generate a google api key, if you are not sure on how to generate the google api key visit the link https://developers.google.com/maps/documentation/android/start

if any problem comment……..

These are some of the screenshots…

screenshot screenshot2 screenshot3

o
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s