MainActivity.java
package com.project.locationinfofinder;
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity implements LocationListener {
GoogleMap googlemap;
MarkerDataSource data;
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (IsGooglePlay()) {
setContentView(R.layout.activity_main);
}
SupportMapFragment mf = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
googlemap = mf.getMap();
googlemap.setMyLocationEnabled(true);
googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
String provider = lm.getBestProvider(new Criteria(), true);
if (provider == null) {
onProviderDisabled(provider);
}
// addMarker();// marker
googlemap
.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(final LatLng Latlang) {
// TODO Auto-generated method stub
LayoutInflater li = LayoutInflater.from(context);
final View v = li.inflate(R.layout.alertlayout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(
context);
builder.setView(v);
builder.setCancelable(false);
builder.setPositiveButton("create",
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
EditText title = (EditText) v
.findViewById(R.id.ettitle);
EditText snippet = (EditText) v
.findViewById(R.id.etsnippet);
googlemap.addMarker(new MarkerOptions()
.title(title.getText()
.toString())
.snippet(
snippet.getText()
.toString())
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
.position(Latlang));
}
});
builder.setNegativeButton("Cancel",
new OnClickListener() {
@Override
public void onClick(DialogInterface dailog,
int which) {
// TODO Auto-generated method stub
dailog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
data = new MarkerDataSource(context);
try {
data.open();
} catch (Exception e) {
// TODO: handle exception
Log.i("hello", "hello");
}
List<MarkerObj> m = data.getMyMarkers();
for (int i = 0; i < m.size(); i++) {
String[] slatlan = m.get(i).getPosition().split(" ");
LatLng lat = new LatLng(Double.valueOf(slatlan[0]),
Double.valueOf(slatlan[1]));
googlemap.addMarker(new MarkerOptions().title(m.get(i).getTitle())
.snippet(m.get(i).getSnippet()).position(lat)
);
}
data.addMarker(new MarkerObj("title", "snippet", "27.7000 85.3500"));
data.close();
}
// private void addMarker() {// marker
// // TODO Auto-generated method stub
// LatLng pos = new LatLng(27.7000, 85.3500);
// googlemap.addMarker(new MarkerOptions()
// .title("lalitpur")
// .snippet("middel of lalitpur")
// .icon(BitmapDescriptorFactory
// .defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
// .position(pos));
//
// }
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("GPS is disabled");
builder.setCancelable(false);
builder.setPositiveButton("Enable GPS", new OnClickListener() {
@Override
public void onClick(DialogInterface dailog, int which) {
// TODO Auto-generated method stub
Intent StartGPS = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(StartGPS);
}
});
builder.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dailog, int which) {
// TODO Auto-generated method stub
dailog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
private boolean IsGooglePlay() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status == ConnectionResult.SUCCESS) {
return (true);
} else {
Toast.makeText(this, "Google Play is not available",
Toast.LENGTH_SHORT).show();
}
return (false);
}
}
MarkerDataSource.java
package com.project.locationinfofinder;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class MarkerDataSource {
MarkerDb dbhelper;
SQLiteDatabase db;
String[] cols = {MarkerDb.TITLE, MarkerDb.SNIPPET, MarkerDb.POSITION};
public MarkerDataSource(Context c) {
// TODO Auto-generated constructor stub
dbhelper = new MarkerDb(c);
}
public void open() throws SQLException{
db = dbhelper.getWritableDatabase();
}
public void close() {
db.close();
}
public void addMarker(MarkerObj m){
ContentValues v = new ContentValues();
v.put(MarkerDb.TITLE, m.getTitle());
v.put(MarkerDb.SNIPPET, m.getSnippet());
v.put(MarkerDb.POSITION, m.getPosition());
db.insert(MarkerDb.TABLE_NAME, null, v);
}
public List<MarkerObj> getMyMarkers(){
List<MarkerObj> markers = new ArrayList<MarkerObj>();
Cursor cursor = db.query(MarkerDb.TABLE_NAME, cols, null, null, null, null, null);
cursor.moveToFirst();
while (cursor.isAfterLast()) {
MarkerObj m = CursorToMarker(cursor);
markers.add(m);
cursor.moveToNext();
}
cursor.close();
return markers;
}
private MarkerObj CursorToMarker(Cursor cursor) {
// TODO Auto-generated method stub
MarkerObj m = new MarkerObj();
m.setTitle(cursor.getString(0));
m.setSnippet(cursor.getString(1));
m.setPosition(cursor.getString(2));
return m;
}
}
MarkerObj.java
package com.project.locationinfofinder;
public class MarkerObj {
private long id;
private String title;
private String snippet;
private String position;
public MarkerObj() {
}
public MarkerObj(long id, String title, String snippet, String position) {
this.id = id;
this.title = title;
this.snippet = snippet;
this.position = position;
}
public MarkerObj(String title, String snippet, String position) {
this.title = title;
this.snippet = snippet;
this.position = position;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSnippet() {
return snippet;
}
public void setSnippet(String snippet) {
this.snippet = snippet;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}
logcat的
08-06 08:15:10.960: D/dalvikvm(8084): DexOpt: couldn't find field Landroid/content/res/Configuration;.smallestScreenWidthDp
08-06 08:15:10.960: W/dalvikvm(8084): VFY: unable to resolve instance field 27
08-06 08:15:10.960: D/dalvikvm(8084): VFY: replacing opcode 0x52 at 0x0012
08-06 08:15:10.960: D/dalvikvm(8084): VFY: dead code 0x0014-0018 in Lcom/google/android/gms/common/GooglePlayServicesUtil;.b (Landroid/content/res/Resources;)Z
08-06 08:15:11.085: W/dalvikvm(8084): Unable to resolve superclass of Lmaps/p/w; (734)
08-06 08:15:11.085: W/dalvikvm(8084): Link of class 'Lmaps/p/w;' failed
08-06 08:15:11.085: W/dalvikvm(8084): Unable to resolve superclass of Lmaps/ap/as; (6056)
08-06 08:15:11.085: W/dalvikvm(8084): Link of class 'Lmaps/ap/as;' failed
08-06 08:15:11.085: W/dalvikvm(8084): Unable to resolve superclass of Lmaps/af/k; (5085)
08-06 08:15:11.085: W/dalvikvm(8084): Link of class 'Lmaps/af/k;' failed
08-06 08:15:11.085: E/dalvikvm(8084): Could not find class 'maps.af.k', referenced from method maps.ag.an.a
08-06 08:15:11.085: W/dalvikvm(8084): VFY: unable to resolve new-instance 4928 (Lmaps/af/k;) in Lmaps/ag/an;
08-06 08:15:11.085: D/dalvikvm(8084): VFY: replacing opcode 0x22 at 0x0091
08-06 08:15:11.109: D/dalvikvm(8084): VFY: dead code 0x0093-00a1 in Lmaps/ag/an;.a (Landroid/view/LayoutInflater;Lcom/google/android/gms/maps/GoogleMapOptions;Z)Lmaps/ag/an;
08-06 08:15:11.382: D/dalvikvm(8084): GC_CONCURRENT freed 924K, 50% free 3415K/6727K, external 2059K/2108K, paused 5ms+3ms
08-06 08:15:11.515: D/AndroidRuntime(8084): Shutting down VM
08-06 08:15:11.515: W/dalvikvm(8084): threadid=1: thread exiting with uncaught exception (group=0x40015578)
08-06 08:15:11.515: E/AndroidRuntime(8084): FATAL EXCEPTION: main
08-06 08:15:11.515: E/AndroidRuntime(8084): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.locationinfofinder/com.project.locationinfofinder.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
08-06 08:15:11.515: E/AndroidRuntime(8084): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
08-06 08:15:11.515: E/AndroidRuntime(8084): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-06 08:15:11.515: E/AndroidRuntime(8084): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-06 08:15:11.515: E/AndroidRuntime(8084): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-06 08:15:11.515: E/AndroidRuntime(8084): at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 08:15:11.515: E/AndroidRuntime(8084): at android.os.Looper.loop(Looper.java:130)
08-06 08:15:11.515: E/AndroidRuntime(8084): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-06 08:15:11.515: E/AndroidRuntime(8084): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 08:15:11.515: E/AndroidRuntime(8084): at java.lang.reflect.Method.invoke(Method.java:507)
08-06 08:15:11.515: E/AndroidRuntime(8084): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
08-06 08:15:11.515: E/AndroidRuntime(8084): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-06 08:15:11.515: E/AndroidRuntime(8084): at dalvik.system.NativeStart.main(Native Method)
08-06 08:15:11.515: E/AndroidRuntime(8084): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
08-06 08:15:11.515: E/AndroidRuntime(8084): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
08-06 08:15:11.515: E/AndroidRuntime(8084): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
08-06 08:15:11.515: E/AndroidRuntime(8084): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
08-06 08:15:11.515: E/AndroidRuntime(8084): at com.project.locationinfofinder.MarkerDataSource.CursorToMarker(MarkerDataSource.java:54)
08-06 08:15:11.515: E/AndroidRuntime(8084): at com.project.locationinfofinder.MarkerDataSource.getMyMarkers(MarkerDataSource.java:41)
08-06 08:15:11.515: E/AndroidRuntime(8084): at com.project.locationinfofinder.MainActivity.onCreate(MainActivity.java:110)
08-06 08:15:11.515: E/AndroidRuntime(8084): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-06 08:15:11.515: E/AndroidRuntime(8084): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-06 08:15:11.515: E/AndroidRuntime(8084): ... 11 more
08-06 08:15:17.156: D/dalvikvm(8084): GC_CONCURRENT freed 748K, 47% free 3580K/6727K, external 1771K/2108K, paused 3ms+10ms
08-06 08:15:17.164: D/Cursor(8084): Database path: /data/data/com.project.locationinfofinder/databases/marker_location
08-06 08:15:17.164: D/Cursor(8084): Table name : location
08-06 08:15:17.164: D/Cursor(8084): SQL : SQLiteQuery: SELECT loc_title, loc_snippet, loc_position FROM location
08-06 08:15:17.164: I/dalvikvm(8084): Uncaught exception thrown by finalizer (will be discarded):
08-06 08:15:17.164: I/dalvikvm(8084): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@405aa588 on location that has not been deactivated or closed
08-06 08:15:17.164: I/dalvikvm(8084): at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:620)
08-06 08:15:17.164: I/dalvikvm(8084): at dalvik.system.NativeStart.run(Native Method)
08-06 08:15:48.601: I/Process(8084): Sending signal. PID: 8084 SIG: 9
这些是使用logcat的代码。我找不到合适的解决方案。在某些问题是相同的,在一些答案地图不会加载。我可以消除这个问题。请.thanx寻求帮助
答案 0 :(得分:2)
只需用此
替换您的代码即可public List<MarkerObj> getMyMarkers()
{
List<MarkerObj> markers = new ArrayList<MarkerObj>();
Cursor cursor = db.query(MarkerDb.TABLE_NAME, cols, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast())
{
MarkerObj m = CursorToMarker(cursor);
markers.add(m);
cursor.moveToNext();
}
cursor.close();
return markers;
}