I have latitude and longitude coordinates stored in an sqlite database. I am trying to retrieve these via cursor and place them all on a map. I'm trying my best, but I am new to developing and need a little help. Here's what I have so far:
public class allmapactivity extends MapActivity {
MapController mControl;
GeoPoint GeoP;
MapView mapV;
private SQLiteAdapter mySQLiteAdapter;
Cursor cursor;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mapV = (MapView) findViewById(R.id.mapview);
mapV.displayZoomControls(true);
mapV.setBuiltInZoomControls(true);
mapV.setSatellite(false);
List<Overlay> mapOverlays = mapV.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.pin);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
cursor = mySQLiteAdapter.queueAll5();
GeoPoint point = new GeoPoint((int) (Double.valueOf(SQLiteAdapter.KEY_CONTENT11) *1E6),(int) (Double.valueOf(SQLiteAdapter.KEY_CONTENT12) *1E6));
OverlayItem overlayitem = new OverlayItem(point, "Test", "Test");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
All of this code works when I enter static numbers for my geopoint, except when I try to add data from my cursor it force closes. Any help would be great!!
It is difficult to tell what is going wrong as the code you provided doesn't take data from the cursor.
Generally speaking you should always call the code below before reading from any cursor.
cursor.moveToFirst();
This is because it is not guaranteed to be pointing to a row that is NOT NULL.
If you can provide more code and/or error messages it might be easier to determine exactly what is going wrong.
Related
I'm using a map view and what I want is to mark all the ex. starbucks that near me.
but I don't have any idea how to do this.
Right now all I can do is to view my current location.
Use ItemizedOverlay to display multiple markers in android, to understand how itemized overlay works, and what it is refer following links, and tutorials:
https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/ItemizedOverlay
http://android-coding.blogspot.in/2011/06/using-itemizedoverlay-to-add-marker-on.html
https://github.com/commonsguy/cw-advandroid/tree/master/Maps/ILuvNooYawk/
Try this
public class MyMap extends MapActivity{
MapView mapView;
MapController mapController;
/* positon */
double latitude = Your latitude;
double longitude = Your longitude;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YourLayout);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(true);
mapController = mapView.getController();
mapController.setZoom(8);
GeoPoint pointRabat = new GeoPoint(microDegres(latitude),
microDegres(longitude));
mapController.setCenter(pointRabat);
}
private int microDegres(double value) {
return (int) (value * 1000000);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
for add overlay
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
GeoPoint pointRabat = new GeoPoint(microDegres(latitude),
microDegres(longitude));
OverlayItem overlayitem = new OverlayItem(point, "My Current Location", "My Current Location");
overlay.addOverlay(overlayitem);
mapOverlays.add(overlay);
}
I am working in google maps...
i did the bellow application and it was working fine then suddenly it stoped working and I trace it from the begining and I found that when I add
mapOverlays = mapView.getOverlays();
the application stops working...
I need a help please
public class MapGtugActivity extends MapActivity implements OnClickListener {
MapView mapView ;
MyLocationOverlay compass ;
MapController controller ;
Drawable drawable ;
Drawable drawable2 ;
List<Overlay> mapOverlays ;
GeoPoint point1 ;
GeoPoint point2 ;
GeoPoint point3 ;
MapItemizedOverlay custom ;
MapItemizedOverlay custom2 ;
//List <GeoPoint> LGP ;
GeoPoint LGP[] = new GeoPoint[6] ;
Button AddLayers ;
int x , y ;
GeoPoint touchedpoint ;
long start ;
long stop ;
CharSequence[] items = {"Layer1", "Layer2"};
boolean[] itemsChecked = new boolean[items.length];
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapview = (MapView) findViewById(R.id.mapview) ;
mapview.setBuiltInZoomControls(true) ;
mapOverlays = mapView.getOverlays();
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
You can only make calls to getOverlays after you have added overlays. I think its returning nullpointerException at that point (because ur calling it in onCreate). Because when getOverlays() is called there is no overlay defined on ur map for it to get.
So I'm just trying to create a generic mapView in android and put a few geopoints in it. I've followed the instructions from the android developer website and had no success... can anyone help? here is my code.
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
// defines the bounds for the overlayItems
super(boundCenterBottom(defaultMarker));
mContext = context;
}
#Override
protected OverlayItem createItem(int i) {
// returns the correct ArrayList position from int i
return mOverlays.get(i);
}
#Override
public int size() {
// returns number of items in ArrayList
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
//Here's my main class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maptastic);
((TextView)((FrameLayout)((LinearLayout)((ViewGroup) getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);
setTitle("Field Trip");
Drawable drawable = this.getResources().getDrawable(R.drawable.supaaaa);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
//initControls();
MapView mapview = (MapView) findViewById(R.id.mapView);
mapview.setBuiltInZoomControls(true);
mcontroller = mapview.getController();
// Plot all geo points
for (Entry<Integer, FieldTripStop> cur : Statics.fieldTripStops.entrySet())
{
// get the FieldTripStop object from the current hash table entry
Statics.currentFTStop = cur.getValue();
// concatenate numbers before (all) and after (6) the decimal, since
// geopoints only accept 6 numbers past the decimal.
theLat = Statics.currentFTStop.latitude;
theLong = Statics.currentFTStop.longitude;
/*
* String manipulation method
*
* theLat = theLat.replace(".","");
* theLat = theLat.substring(0, 8);
* theLong = theLong.replace(".","");
* theLong = theLong.substring(0, 8);
* point = new GeoPoint((int)(Integer.valueOf(theLat)), (int)(Integer.valueOf(theLong)));
*/
theDLat = Double.parseDouble(theLat);
theDLong = Double.parseDouble(theLong);
//olay = new OverlayItem(point, "herp", "derp");
//olayitems.add(olay);
List<Overlay> mapOverlays = mapview.getOverlays();
point = new GeoPoint((int)(theDLat*1E6), (int)(theDLong*1E6));
OverlayItem overlayitem = new OverlayItem(point, "holda, Mundo!", "I'm in Mexico City~!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
You need to a) use only a single HelloItemizedOverlay (instead one for each point) and b) add this overlay to the map before you add the markers (In your current code, the overlay is not yet associated with the map when populate is called, so the map does not receive any notification and doesn't refresh itself).
Some things to check:
Did you specify your api key for the MapView? *
*) How to specify your api key (in the layout resource for your activity):
<com.google.android.maps.MapView android:id="#+id/map"
...
android:apiKey="your-api-key-here"/>
On my application i receive coordinates from a remote server and i want to mark the location on the coordinates on a map, this happens on demand inside onClick method. The problem is that when i update the location i end up with multiple markers on the map instead of just one, the current location. is there any way to remove the previous marker before adding the next one?
I followed the steps in this tutorial : http://developer.android.com/resources/tutorials/views/hello-mapview.html
And my code goes like this :
public class AppTwoAndroid extends MapActivity {
private Button refreshButton;
double lat, lon;
ConnectionHandler conhandler;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
Log.i("AppTwo", "making connectionhandler object");
conhandler = new ConnectionHandler();
conhandler.execute();
Log.i("AppTwo", "making button");
this.refreshButton = (Button)this.findViewById(R.id.close);
final List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
final AppTwoAndroidItemizedOverlay itemizedoverlay = new AppTwoAndroidItemizedOverlay(drawable);
refreshButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("AppTwo", "inside onclick");
if (mapOverlays.contains(itemizedoverlay) == true) {
mapOverlays.remove(itemizedoverlay);
}
conhandler.write();
lat = conhandler.retLat();
lon = conhandler.retLon();
lat = lat * 1e6;
lon = lon * 1e6;
int ilat = (int) lat;
int ilon = (int) lon;
GeoPoint point = new GeoPoint(ilat ,ilon);
OverlayItem overlayitem = new OverlayItem(point, null, "AppOne");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
Toast.makeText(getBaseContext(), "lat is: " + lat + " lon is: " + lon
, Toast.LENGTH_SHORT).show();
}
});
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
and my AppTwoAndroidItemizedOverlay class is :
public class AppTwoAndroidItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public AppTwoAndroidItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
public AppTwoAndroidItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
}
In the onClick method just give
mapOverlays.clear();
this should clear any existing markers.
Hope this works :)
In this part of code:
if (mapOverlays.contains(itemizedoverlay) == true) {
mapOverlays.remove(itemizedoverlay);
}
When your removing the Overlay from the mapOverlay structure you are not really clearing the overlay, so when you add other item and re-add it to the mapOverlay there will be 2 markers.
If you just want a single marker do an Overlay that has setOverlayItem instead of a list with an 'adding' logic. (meaning do a overlay with just an item that when you add another, just replaces the old one)
Hope it helped! :D
Add this statement before the code....
mMap.clear();
mMap.clear();
LatLng latLng = new LatLng(gps.getLatitude(), gps.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(gps.getLatitude(), gps.getLongitude()), 15));
mMap.addMarker(new MarkerOptions().position(latLng).title(""));
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
I have developing apps with google map .I follow material from android developers site and some other guidelines .wen i run the program the icon only point to the location .in background no map view displayed.can any one help to me ?
Thanks in advance
Regards
Lakshmanan.
Here is my source code,
public class MapPage extends MapActivity
{
MapView mapView;
MapController mc;
GeoPoint p;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mc = mapView.getController();
String coordinates[] = {"1.352566007", "103.78921587"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(1);
mapView.invalidate();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
java class :
public class HelloItemizedOverlay extends ItemizedOverlay
{
Context mContext;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
}
My suspicion is that you are not specifying the correct google maps key. You need to specify the google maps key that is generated for the keystore you are signing your application with.
I don't see the key being set in the code (You need to set it in the constructor of the MapView or using the attribute android:apiKey if you are constructing a MapView from an xml layout). If you don't do this you usually end up with a gray screen, the overlays and then a Google watermark in the lower left hand corner, which I am guessing you are getting.
Here is the link for the place to get a map key.
Sign Up for the Android Maps Api
I also think that you need to include the Maps API key. I prefer the inclusion within a seperate map.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- The Api Key needs to be replaced corresponding to the signing certificate.
Check this site for more info:
http://code.google.com/intl/ko/android/maps-api-signup.html -->
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myMap"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:enabled="true" android:clickable="true"
android:apiKey="#+string/maps_api_key" />
Then I include this file in any of my map views like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myMapView" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="#layout/map" />
</RelativeLayout>