Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have tried tis code but got an error unfortunatelly stopped.Any one help me

You should post a comment at the very blog page.
Share this answer
anamicaa 20-May-14 5:40am    
<relativelayout xmlns:android=""
="" xmlns:tools="" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">

="" android:layout_width="match_parent" android:layout_height="match_parent">

anamicaa 20-May-14 5:40am    
package in.wptrafficanalyzer.locationmarkersqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class LocationsDB extends SQLiteOpenHelper{

/** Database name */
private static String DBNAME = "locationmarkersqlite";

/** Version number of the database */
private static int VERSION = 1;

/** Field 1 of the table locations, which is the primary key */
public static final String FIELD_ROW_ID = "_id";

/** Field 2 of the table locations, stores the latitude */
public static final String FIELD_LAT = "lat";

/** Field 3 of the table locations, stores the longitude*/
public static final String FIELD_LNG = "lng";

/** Field 4 of the table locations, stores the zoom level of map*/
public static final String FIELD_ZOOM = "zom";

/** A constant, stores the the table name */
private static final String DATABASE_TABLE = "locations";

/** An instance variable for SQLiteDatabase */
private SQLiteDatabase mDB;

/** Constructor */
public LocationsDB(Context context) {
super(context, DBNAME, null, VERSION);
this.mDB = getWritableDatabase();

/** This is a callback method, invoked when the method getReadableDatabase() / getWritableDatabase() is called
* provided the database does not exists
* */
public void onCreate(SQLiteDatabase db) {
String sql = "create table " + DATABASE_TABLE + " ( " +
FIELD_ROW_ID + " integer primary key autoincrement , " +
FIELD_LNG + " double , " +
FIELD_LAT + " double , " +
FIELD_ZOOM + " text " +
" ) ";


/** Inserts a new location to the table locations */
public long insert(ContentValues contentValues){
long rowID = mDB.insert(DATABASE_TABLE, null, contentValues);
return rowID;

/** Deletes all locations from the table */
public int del(){
int cnt = mDB.delete(DATABASE_TABLE, null , null);
return cnt;

/** Returns all the locations from the table */
public Cursor getAllLocations(){
return mDB.query(DATABASE_TABLE, new String[] { FIELD_ROW_ID, FIELD_LAT , FIELD_LNG, FIELD_ZOOM } , null, null, null, null, null);

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

anamicaa 20-May-14 5:41am    
package in.wptrafficanalyzer.locationmarkersqlite;

import java.sql.SQLException;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;

/** A custom Content Provider to do the database operations */
public class LocationsContentProvider extends ContentProvider{

public static final String PROVIDER_NAME = "in.wptrafficanalyzer.locationmarkersqlite.locations";

/** A uri to do operations on locations table. A content provider is identified by its uri */
public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/locations" );

/** Constant to identify the requested operation */
private static final int LOCATIONS = 1;

private static final UriMatcher uriMatcher ;

static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "locations", LOCATIONS);

/** This content provider does the database operations by this object */
LocationsDB mLocationsDB;

/** A callback method which is invoked when the content provider is starting up */
public boolean onCreate() {
mLocationsDB = new LocationsDB(getContext());
return true;

/** A callback method which is invoked when insert operation is requested on this content provider */
public Uri insert(Uri uri, ContentValues values) {
long rowID = mLocationsDB.insert(values);
Uri _uri=null;
_uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
}else {
try {
throw new SQLException("Failed to insert : " + uri);
} catch (SQLException e) {
return _uri;

public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;

/** A callback method which is invoked when delete operation is requested on this content provider */
public int delete(Uri uri, String selection, String[] selectionArgs) {
int cnt = 0;
cnt = mLocationsDB.del();
return cnt;

/** A callback method which is invoked by default content uri */
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

return mLocationsDB.getAllLocations();
return null;

public String getType(Uri uri) {
return null;
anamicaa 20-May-14 5:41am    
package in.wptrafficanalyzer.locationmarkersqlite;

import android.content.ContentValues;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;


public class MainActivity extends FragmentActivity implements LoaderCallbacks<cursor> {

GoogleMap googleMap;

protected void onCreate(Bundle savedInstanceState) {

// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

// Showing status
if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available

int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);;

}else { // Google Play Services are available

// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(;

// Getting GoogleMap object from the fragment
googleMap = fm.getMap();

// Enabling MyLocation Layer of Google Map

// Invoke LoaderCallbacks to retrieve and draw already saved locations in map
getSupportLoaderManager().initLoader(0, null, this);

googleMap.setOnMapClickListener(new OnMapClickListener() {

public void onMapClick(LatLng point) {

// Drawing marker on the map

// Creating an instance of ContentValues
ContentValues contentValues = new ContentValues();

// Setting latitude in ContentValues
contentValues.put(LocationsDB.FIELD_LAT, point.latitude );

// Setting longitude in ContentValues
contentValues.put(LocationsDB.FIELD_LNG, point.longitude);

// Setting zoom in ContentValues
contentValues.put(LocationsDB.FIELD_ZOOM, googleMap.getCameraPosition().zoom);

// Creating an instance of LocationInsertTask
LocationInsertTask insertTask = new LocationInsertTask();

// Storing the latitude, longitude and zoom level to SQLite database

Toast.makeText(getBaseContext(), "Marker is added to the Map", Toast.LENGTH_SHORT).show();

googleMap.setOnMapLongClickListener(new OnMapLongClickListener() {
public void onMapLongClick(LatLng point) {

// Removing all markers from the Google Map

// Creating an instance of LocationDeleteTask
LocationDeleteTask deleteTask = new LocationDeleteTask();

// Deleting all the rows from SQLite database table
anamicaa 20-May-14 5:41am    

<manifest xmlns:android=""
="" package="in.wptrafficanalyzer.locationmarkersqlite" android:versioncode="1" android:versionname="1.0">

="" android:targetsdkversion="17">


<uses-permission android:name="in.wptrafficanalyzer.locationmarkersqlite.permission.MAPS_RECEIVE">

<uses-permission android:name="android.permission.INTERNET">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
<uses-permission android:name="">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">

="" android:required="true">

="" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
="" android:label="@string/app_name">
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">

android:exported="false" />

android:value="YOUR_ANDROID_API_KEY" />

Hi anamicaa,
have You refer LogCat??
It gives more help in this state.. :)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900