I did as you told me.I would lie to ask inside the oncreate() should a put something except from
mapOverlays = mapView.getOverlays(); ??
Also, at the start of my mainactivity class, where i declare all the variables, what should i put? I have :
List<Overlay> mapOverlays;
HelloItemizedOverlay itemizedoverlay0;
HelloItemizedOverlay itemizedoverlay1;
HelloItemizedOverlay itemizedoverlay2;
HelloItemizedOverlay itemizedoverlay3;
HelloItemizedOverlay itemizedoverlay4;
HelloItemizedOverlay itemizedoverlay5;
GeoPoint point;
Drawable drawable0;
Drawable drawable1;
Drawable drawable2;
Drawable drawable3;
Drawable drawable4;
Drawable drawable5;
OverlayItem overlayItem5;
OverlayItem overlayItem0;
OverlayItem overlayItem1;
OverlayItem overlayItem2;
OverlayItem overlayItem3;
OverlayItem overlayItem4;`
Is there a problem to use the same geopoint variable(point) for all the categories of markers?
In order to add the overlay items to each category i did:
try {
Drawable drawable0 = this.getResources().getDrawable(R.drawable.marker_default);
HelloItemizedOverlay itemizedoverlay0 = new HelloItemizedOverlay(drawable0, this);
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
double d1 =json.getDouble("longtitude");
double d2 =json.getDouble("latitude");
GeoPoint point = new GeoPoint((int) (d2 * 1E6),
(int) (d1 * 1E6));
OverlayItem overlayItem0= new OverlayItem((GeoPoint) point, "Center", "Center");
itemizedoverlay0.addOverlay(overlayItem0);
}//for
mapOverlays.add(itemizedoverlay0);
mapView.getController().animateTo(point);
mc.setZoom(7);
}//try
Also the mapOverlays.remove(itemizedoverlay0); does not work..Thanks!
It still doesnt work.. In the rm0() function i think is the problem. I Write:
public void rm0()
{
for(int i=0; i< mapOverlays.size();i++)
{
Toast.makeText(getBaseContext(), "in the rm0"+i,Toast.LENGTH_SHORT).show();
mapOverlays.remove(overlayItem0);
}
}
How can i delete the itemizedOverlay0 only?? I have tried itemizedoverlay0.clearOverlay(); too but nothing happens..
Thanks!
You are missing call populate() after changing the items in the ItemizedOverlay.
Change the methods as per bellow:
public void removeOverlay(int item) {
mOverlays.remove(item);
populate();
}
public void clear() {
mOverlays.clear();
populate();
}
--EDITED--
HelloItemizedOverlay itemizedoverlay0 = new HelloItemizedOverlay(drawable0, this);
try {
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
double d1 =json.getDouble("longtitude");
double d2 =json.getDouble("latitude");
Drawable drawable0 = this.getResources().getDrawable(R.drawable.marker_default);
GeoPoint point = new GeoPoint((int) (d2 * 1E6), (int) (d1 * 1E6));
OverlayItem overlayItem0= new OverlayItem((GeoPoint) point, "Center", "Center");
itemizedoverlay0.addOverlay(overlayItem0);
}
mapOverlays.add(itemizedoverlay0);
mapView.getController().animateTo(point);
mc.setZoom(7);
}
Regards
Related
I have an application where I fetch longitude and latitude from my database and want to display it in a MapView. I am able to show the one stored most recently, but I think some array is needed in a for loop to add it in an overlay. I don't know how to implement it though.
Here is my buttonClick on which it will fetch lat/long and display it in a MapView:
buttonShowMarkers = (Button) findViewById(R.id.button_SHOW_MARKERS);
buttonShowMarkers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
db = openOrCreateDatabase("LocationFetch.db", MODE_PRIVATE , null);
cursor = db.rawQuery("SELECT * FROM user" , null);
cursor.moveToFirst();
do {
int dataID = cursor.getColumnIndex("id");
String dataString = cursor.getString(0);
Log.d("ID DATA FROM DATABASE---->" , dataString);
String dataNAME = cursor.getString(1).toString().trim();
Log.d("NAME DATA FROM DATABASE---->", dataNAME);
String dataLAT = cursor.getString(2).toString().trim();
Log.d("LAT DATA FROM DATABASE----->" , dataLAT);
int latitude = cursor.getColumnIndex("latitude");
String dataLON = cursor.getString(3).toString().trim();
Log.d("LON DATA FROM DATABASE----->" , dataLON);
int longitude = cursor.getColumnIndex("longitude");
showLatLon(latitude , longitude , dataNAME);
// ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
/* List<Overlay> ListOverlays = mapView.getOverlays();
* OverlayItem[] markerItem = {new OverlayItem(new GeoPoint(latitude, longitude) , ""+dataNAME , ""+dataString)};
* OverlayItem markerItem = new OverlayItem(new GeoPoint((int)(latitude *1E6), (int)(longitude *1E6)) , ""+dataNAME , ""+dataString);
* drawableOne = getResources().getDrawable(R.drawable.location_blue);
* helloItemizedOverlay = new HelloItemizedOverlay(drawableOne);
* helloItemizedOverlay.addOverlayItem(markerItem);
* mapView.getOverlays().add(helloItemizedOverlay);
* ListOverlays.add(myItemizedOverlay);*/
//for(int i = dataID ; )
} while (cursor.moveToNext());
/*String[] arrLat = new String[]{LATdata};
String[] arrLon = new String[]{LONdata};*/
//showLatLon(dataLAT , dataLON);
cursor.moveToNext();
} catch(SQLException e) {
e.printStackTrace();
} finally {
cursor.close();
}
db.close();
}
private void showLatLon(int latitude , int longitude , String nAMEdata) {
// GeoPoint point = new GeoPoint( (latitude) , (latitude) );
List<Overlay> ListOverlays = mapView.getOverlays();
//OverlayItem[] markerItem = {new OverlayItem(new GeoPoint(latitude, longitude) , ""+dataNAME , ""+dataString)};
OverlayItem markerItem = new OverlayItem(new GeoPoint(latitude,latitude ) , ""+nAMEdata , null);
drawableOne = getResources().getDrawable(R.drawable.location_blue);
helloItemizedOverlay = new HelloItemizedOverlay(drawableOne);
helloItemizedOverlay.addOverlayItem(markerItem);
mapView.getOverlays().add(helloItemizedOverlay);
ListOverlays.add(myItemizedOverlay);
}
});
You can try using this solution -
Create a class named LatLonPoint - this basically does the conversion from latlon to GeoPoint -
public class LatLonPoint extends GeoPoint {
public LatLonPoint(double latitude, double longitude) {
super((int) (latitude * 1E6), (int) (longitude * 1E6));
}
}
Create a Itemized Overlay -
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(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();
}
#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
public boolean onKeyDown(int keyCode, KeyEvent event, MapView mapView) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
}
return super.onKeyDown(keyCode, event, mapView);
}
}
Now in the main activity -
try {
db = openOrCreateDatabase("LocationFetch.db", MODE_PRIVATE , null);
cursor = db.rawQuery("SELECT * FROM user" , null);
cursor.moveToFirst();
do {
int dataID = cursor.getColumnIndex("id");
String dataString = cursor.getString(0);
Log.d("ID DATA FROM DATABASE---->" , dataString);
String dataNAME = cursor.getString(1).toString().trim();
Log.d("NAME DATA FROM DATABASE---->", dataNAME);
String dataLAT = cursor.getString(2).toString().trim();
Log.d("LAT DATA FROM DATABASE----->" , dataLAT);
int latitude = cursor.getColumnIndex("latitude");
String dataLON = cursor.getString(3).toString().trim();
Log.d("LON DATA FROM DATABASE----->" , dataLON);
int longitude = cursor.getColumnIndex("longitude");
// Add the item here --
itemizedoverlay.addOverlay(new OverlayItem(new LatLonPoint(latitude, longitude), dataNAME, null));
//showLatLon(latitude , longitude , dataNAME);
// ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
/* List<Overlay> ListOverlays = mapView.getOverlays();
* OverlayItem[] markerItem = {new OverlayItem(new GeoPoint(latitude, longitude) , ""+dataNAME , ""+dataString)};
* OverlayItem markerItem = new OverlayItem(new GeoPoint((int)(latitude *1E6), (int)(longitude *1E6)) , ""+dataNAME , ""+dataString);
* drawableOne = getResources().getDrawable(R.drawable.location_blue);
* helloItemizedOverlay = new HelloItemizedOverlay(drawableOne);
* helloItemizedOverlay.addOverlayItem(markerItem);
* mapView.getOverlays().add(helloItemizedOverlay);
* ListOverlays.add(myItemizedOverlay);*/
//for(int i = dataID ; )
} while (cursor.moveToNext());
// Add all itemized overlay here
mapOverlays.add(itemizedoverlay);
/*String[] arrLat = new String[]{LATdata};
String[] arrLon = new String[]{LONdata};*/
//showLatLon(dataLAT , dataLON);
//cursor.moveToNext();
} catch(SQLException e) {
e.printStackTrace();
} finally {
cursor.close();
}
For the above block you have to initialize these two items beffore the do.. while -
List<Overlay> mapOverlays = mMapView.getOverlays();
and
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
drawable, this);
EDIT-- Adding iterating Cursor Part -
Drawable drawable = this.getResources().getDrawable(
R.drawable.ic_launcher);
List<Overlay> mapOverlays = mMapView.getOverlays();
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
drawable, this);
while(cursor.moveToNext())
{
// get the values --
int dataID = cursor.getColumnIndex("id");
String dataString = cursor.getString(0);
Log.d("ID DATA FROM DATABASE---->" , dataString);
String dataNAME = cursor.getString(1).toString().trim();
Log.d("NAME DATA FROM DATABASE---->", dataNAME);
String dataLAT = cursor.getString(2).toString().trim();
Log.d("LAT DATA FROM DATABASE----->" , dataLAT);
int latitude = cursor.getColumnIndex("latitude");
String dataLON = cursor.getString(3).toString().trim();
Log.d("LON DATA FROM DATABASE----->" , dataLON);
int longitude = cursor.getColumnIndex("longitude");
// Assuming your logic for retrieving values is correct
// Add into the itemized Overlay here
itemizedoverlay.addOverlay(new OverlayItem(new LatLonPoint(latitude, longitude), dataNAME, null));
}
mapOverlays.add(itemizedoverlay);
What i do is to take data from an xml file and then using a Custom ItemizedOverlay i add the items.
public CustomItemizedOverlay callme(List<Places> result){
Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
latitudeE6 = result.get(i).latitude;
longitudeE6 = result.get(i).longitude;
GeoPoint point = new GeoPoint(latitudeE6a, longitudeE6a);
OverlayItem overlayitem = new OverlayItem(point, result.get(i).title, result.get(i).text);
CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable);
itemizedOverlay.addOverlay(overlayitem);//add point
i++;
latitudeE6 =0;
longitudeE6 =0;
return itemizedOverlay;
}
While i am on the UI Thread i use a while() loop to print :
int j=0;
List<Overlay> mapOverlays = mapView.getOverlays();
while( j < 5 ) {
test1 = callme(result1);
mapOverlays.add(test1);
j++;
}
MapController mapController = mapView.getController();
mapController.setZoom(12);
Having to display only five items the loop stops while j < 5 .
The problem is that it only displays the last item from the xml data.
Step1:
Add this java file..
public class MapItemizedOverlay extends ItemizedOverlay<OverlayItem>
{
public static boolean isRouteSelecting;
public static ArrayList<GeoPoint> geoPoints;
private GeoPoint geoPoint_;
private ArrayList<OverlayItem> overlayItems_ = new ArrayList<OverlayItem>();
public MapItemizedOverlay(Drawable defaultMarker)
{
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public void addOverlay(OverlayItem overlayItem)
{
overlayItems_.add(overlayItem);
populate();
}
#Override
protected OverlayItem createItem(int i)
{
// TODO Auto-generated method stub
return overlayItems_.get(i);
}
#Override
public int size()
{
// TODO Auto-generated method stub
return overlayItems_.size();
}
}
Step2:
Into your MapActivity...(oncreate)
mapItemizedOverlay_ = new MapItemizedOverlay(drawable_);
for(int i = 0; i < yourList.size(); i++)
{
GeoPoint geoPoint = new GeoPoint(Latitude, Longitude);
OverlayItem overlayItem = new OverlayItem(geoPoint, busStop.getStopName(), "");
mapItemizedOverlay_.addOverlay(overlayItem);
}
drawable_ = this.getResources().getDrawable(R.drawable.ic_launcher);
mapView_.getOverlays().add(mapItemizedOverlay_);
You missed a loop for int i in your callme(List<Places> result) method
Update:
List<Overlay> mapOverlays = mapView.getOverlays();
for(int i=0;i<result1.size();i++)
{
Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
latitudeE6 = result1.get(i).latitude;
longitudeE6 = result1.get(i).longitude;
GeoPoint point = new GeoPoint(latitudeE6a,longitudeE6a);
OverlayItem overlayitem = new OverlayItem(point, result1.get(i).title, result1.get(i).text);
CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable);
itemizedOverlay.addOverlay(overlayitem); //add point
mapOverlays.add(itemizedOverlay);
}
latitudeE6 =0;
longitudeE6 =0;
MapController mapController = mapView.getController();
mapController.setZoom(12);
Use the above code remove your code what have you post with question and let mek now what happen..
So first I checked if there is a Goecoder service present in or not....
Then I use this service and check the result from getFromLocationName, and then i make the point of it which I add to my overlay and display it...But for some reason, it does not display anything.
private void convert_Points() throws IOException {
// initialization of overlays
mapOverlays = mapV.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.pin);
itemizedoverlay = new ItemizedOverlay(drawable);
// check if getFromLocationName is present or not
if (!Geocoder.isPresent()){
Toast.makeText(getBaseContext(),"Sorry! Geocoder service not Present.", Toast.LENGTH_LONG).show();
}
else{
// make OverlayItem by creating a GeoPoint that defines our map coordinates
Geocoder geocoder = new Geocoder(this);
List<Address> geoResults = geocoder.getFromLocationName("Empire State Building", 5,-44.00, 111.00, -12.0, 155.0);
Address location = geoResults.get(0);
location.getLatitude();
location.getLongitude();
String s=Double.toString(geoResults.get(0).getLatitude());
Toast.makeText(getBaseContext(),s, Toast.LENGTH_LONG).show();
GeoPoint point= new GeoPoint( (int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));
OverlayItem overlayitem = new OverlayItem(point, "", "");
// add this OverlayItem to a collection in the ItemizedOverlay
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
}
I want to remove same overlay at the specific position. This is my code for add the overlay, but how can get the position for every overlay?
public void visual_overlay(List<DatiOverlay> list_overlay){
CustomOverlay itemized_overlays = null;
GeoPoint point = null;
for(int ov=0;ov<list_overlay.size();ov++){
itemized_overlays = new CustomOverlay(drawable,mappa, Integer.toString(list_overlay.get(ov).get_id_segnalazione()),list_overlay.get(ov).get_via(),list_overlay.get(ov).get_citta(),list_overlay.get(ov).get_descrizione(),list_overlay.get(ov).get_cek_feed());
point = new GeoPoint((int)(list_overlay.get(ov).get_lat() * 1E6), (int)(list_overlay.get(ov).get_lng() * 1E6));
OverlayItem overlayItem = new OverlayItem(point, list_overlay.get(ov).get_nome(), list_overlay.get(ov).get_prezzo());
itemized_overlays.addOverlay(overlayItem);
mappa.invalidate();
mappa.getOverlays().add(itemized_overlays);
}
You remove an overlay from a MapView like this:
List<Overlay> mapOverlays = mapView.getOverlays();
if (mapOverlay != null) mapOverlays.remove(someOverlay);
I had shown latitudes and longitudes in Google map when i click marker it display a balloon on which gallery name and address are written
I want when I click on balloon that gallery name are fetched how to achieve this below is my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.map);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
drawable1 = getResources().getDrawable(R.drawable.greenballon);
itemizedOverlay = new MyItemizedOverlay(drawable1, mapView);
drawable2 = getResources().getDrawable(R.drawable.blueballon);
itemizedOverlay2 = new MyItemizedOverlay(drawable2, mapView);
drawable3 = getResources().getDrawable(R.drawable.redballon);
itemizedOverlay3 = new MyItemizedOverlay(drawable3, mapView);
for(int i=0;i<20;i++)
{
if(sitesList.getType().get(i).equalsIgnoreCase("Centros Culturales"))
{
name = sitesList.getLatitude().get(i);
name1 = sitesList.getLongitude().get(i);
Log.i("centos culturales lat" + i,name);
Log.i("culturarls longitiitude"+i,name1);
point = new GeoPoint((int) (Double.parseDouble(name) * 1E6),
(int) (Double.parseDouble(name1) * 1E6));
OverlayItem overlayItem = new OverlayItem(point, sitesList.getGalleryname().get(i),
sitesList.getAddress().get(i));
itemizedOverlay.addOverlay(overlayItem);
}
}
for(int i=0;i<sitesList.getLatitude().size();i++)
{
if(sitesList.getType().get(i).equalsIgnoreCase("Centros Culturales"))
{
name = sitesList.getLatitude().get(i);
name1 = sitesList.getLongitude().get(i);
Log.i("centos culturales lat" + i,name);
Log.i("culturarls longitiitude"+i,name1);
point = new GeoPoint((int) (Double.parseDouble(name) * 1E6),
(int) (Double.parseDouble(name1) * 1E6));
OverlayItem overlayItem = new OverlayItem(point, sitesList.getGalleryname().get(i),
sitesList.getAddress().get(i));
itemizedOverlay.addOverlay(overlayItem);
}
}
for(int i=0;i<sitesList.getLatitude().size();i++)
{
if(sitesList.getType().get(i).equalsIgnoreCase("Residencias"))
{
name = sitesList.getLatitude().get(i);
name1 = sitesList.getLongitude().get(i);
Log.i("residencias latitute" + i,name);
Log.i("residencias longitiitude"+i,name1);
point3 = new GeoPoint((int) (Double.parseDouble(name) * 1E6),
(int) (Double.parseDouble(name1) * 1E6));
OverlayItem overlayItem = new OverlayItem(point3, sitesList.getGalleryname().get(i),
sitesList.getAddress().get(i));
itemizedOverlay2.addOverlay(overlayItem);
}
}
mapOverlays.add(itemizedOverlay);
mapOverlays.add(itemizedOverlay2);
**//actuaaly i show lat long in google map according to "gallery type"..so here how to get data on tap of baloon...??????**
#Override
protected boolean onBalloonTap(int index, OverlayItem item) {
for(int i=0;i<sitesList.getLatitude().size();i++)
{
if(sitesList.getType().get(i).equalsIgnoreCase("Centros Culturales"))
{
array_galleryname1 = new String[sitesList.getGalleryname().size()];
array_galleryurl1 = new String[sitesList.getImagesurl().size()];
galleryname = sitesList.getGalleryname().get(i);
galleryurl = sitesList.getImagesurl().get(i);
array_galleryname1[index] = galleryname;
array_galleryurl1[index] = galleryurl;
}
}
Toast.makeText(c, array_galleryname1[index] + index,
Toast.LENGTH_LONG).show();
for(int i=0;i<sitesList.getLatitude().size();i++)
{ if(sitesList.getType().get(i).equalsIgnoreCase("Residencias"))
{
array_galleryname = new String[sitesList.getGalleryname().size()];
array_galleryurl1 = new String[sitesList.getImagesurl().size()];
galleryname = sitesList.getGalleryname().get(i);
galleryurl = sitesList.getImagesurl().get(i);
array_galleryname[index] = galleryname;
array_galleryurl1[index] = galleryurl;
}
}
You have to override onTap() .
See this link. http://developer.android.com/resources/tutorials/views/hello-mapview.html