How to draw multiple bubbles on the same map

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

How to draw multiple bubbles on the same map

Postby saisri2k2 » Wed Nov 19, 2008 9:33 am

Hello,

I've done the the plotting with only a single bubble, but i'm not able to do it for multiple bubbles. i'm pasting my code below. could you give me suggestions to modify the following code...thanks.. i'm getting a few exceptins if i do it by putting the geopoint as array etc..,I had even attached my whole code below, please chek it by running the code...
Thanks in advance..

package com.mmg;

import java.util.ArrayList;
import java.util.List;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.widget.TextView;
import org.apache.http.util.ByteArrayBuffer;
import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ZoomControls;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;





public class Mapping extends MapActivity
{

//private MapView map1;
private Button btnSearch;
private EditText adress;
private MapView map;
Drawable defaultMarker;
GeoPoint p,q;
GeoPoint pi[];
private double lat=29.5582*1000000;
private double lon=-95.0963*1000000;
private double lata[];
private double lona[];
private Geocoder gc;
int zip[];
int address[];
int problem[];
String message;
int l=0;
int flag=0;
int count=0;
String zipcode[]=new String[]{"one"};
String addressd[]=new String[]{"one"};
String problemd[]=new String[]{"one"};

@Override
public void onCreate(Bundle savedInstanceState)
{



///after connection statements
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map = (MapView) findViewById(R.id.maps); // Get map from XML
btnSearch = (Button) findViewById(R.id.simpleGM_btn_search); // Get button from xml


adress = (EditText) findViewById(R.id.simpleGM_adress); // Get address from XML
gc = new Geocoder(this); // create new geocoder instance

btnSearch.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String addressInput = adress.getText().toString(); // Get input text
//onclick goes here...

try {
//connection string start
String myString = null;
URL myURL1 = new URL(
"http://dcm.uhcl.edu/capf08g5/test/default2.aspx?zipcode="+addressInput);
URL myURL2 = new URL(
"http://dcm.uhcl.edu/capf08g5/test/test.txt");

/* Open a connection to that URL. */
URLConnection ucon1 = myURL1.openConnection();


/* Define InputStreams to read
* from the URLConnection. */
InputStream is = ucon1.getInputStream();
BufferedInputStream bis1 = new BufferedInputStream(is);

/* Read bytes to the Buffer until
* there is nothing more to read(-1). */
ByteArrayBuffer baf1 = new ByteArrayBuffer(500);
int current = 0;
while((current = bis1.read()) != -1){
baf1.append((byte)current);
}

////////////////////////////////////////////////
URLConnection ucon2 = myURL2.openConnection();
InputStream is2 = ucon2.getInputStream();
BufferedInputStream bis2 = new BufferedInputStream(is2);

/* Read bytes to the Buffer until
* there is nothing more to read(-1). */
ByteArrayBuffer baf2 = new ByteArrayBuffer(100);
int current2 = 0;
while((current2 = bis2.read()) != -1){
baf2.append((byte)current2);
}
///////////////////////////////////
/* Convert the Bytes read to a String. */
myString = new String(baf2.toByteArray());

//put problems into array
int k=myString.length();
while(l<k){
int firstc=myString.indexOf(";");
int secondc=myString.indexOf(";",++firstc);
int thirdc=myString.indexOf("#",++secondc);


zipcode[count]=myString.substring(0,firstc-1);
addressd[count]=myString.substring(firstc,secondc-1);
problemd[count]=myString.substring(secondc,thirdc);
myString=myString.substring(thirdc+1,k);
l=thirdc+1;
count++;
}
l=0;



//for(int temp=0;temp<count;temp++)
//{
message="zipcode="+zipcode[0]+" address="+addressd[0]+" problem="+problemd[0]+"";

//}


//put problems into array end

//search for ;

//Connnection string end
//changes
List<Address> foundAdresses = gc.getFromLocationName(addressInput, 5); // Search addresses


//changes
if (foundAdresses.size() == 0) { // if no address found,
Dialog locationError = new AlertDialog.Builder(
Mapping.this).setIcon(0).setTitle(
"Error").setPositiveButton(R.string.ok, null)
.setMessage(
"Sorry, your address doesn't exist.")
.create();
locationError.show();

} else { // else display address on map
for (int i = 0; i < foundAdresses.size(); ++i) {
// Save results as Longitude and Latitude
// @todo: if more than one result, then show a
// select-list

//changes
int temp1=0;
while(temp1<count){
List<Address> foundAdresses1 = gc.getFromLocationName(addressd[temp1], 5); // Search multiple addresses
for(int l=0;l<foundAdresses1.size();++l){
Address t=foundAdresses1.get(l);
lata[temp1]=t.getLatitude();
lona[temp1]=t.getLongitude();
lata[temp1]=lata[temp1]*1000000;
lona[temp1]=lona[temp1]*1000000;
pi[temp1]= new GeoPoint((int)lata[temp1],(int)lona[temp1]);
temp1++;
}
}
//changes
Address x = foundAdresses.get(i);
lat = x.getLatitude();
lon = x.getLongitude();
lat=lat*1000000;
lon=lon*1000000;
p = new GeoPoint((int)lat,(int)lon);


ZoomControls zoomControls = (ZoomControls) map.getZoomControls();
zoomControls.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

map.addView(zoomControls);
map.displayZoomControls(true);



defaultMarker = getResources().getDrawable(R.drawable.bubble);

defaultMarker.setBounds(0, 0, defaultMarker.getIntrinsicWidth(), defaultMarker.getIntrinsicHeight());

map.getOverlays().add(new MapOverlay(defaultMarker));


MapController mc = map.getController();

mc.animateTo(p);

mc.setZoom(15);

}

}

}catch (Exception e) {
Dialog locationError = new AlertDialog.Builder(
Mapping.this).setIcon(0).setTitle(
"Error").setPositiveButton(R.string.ok, null)
.setMessage(e.getMessage()
)
.create();
locationError.show();
// : Show error message
}//end try and ctch
}
});


}


private class MapOverlay extends ItemizedOverlay<OverlayItem>
{
private ArrayList<OverlayItem> mItemList = new ArrayList<OverlayItem>();


@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{

super.draw(canvas, mapView, shadow);
MapOverlay.boundCenterBottom(defaultMarker);


}

public MapOverlay(Drawable pMarker)
{
super(pMarker);
int temp2=0;
while(temp2<count){
mItemList.add(new OverlayItem(pi[temp2], "Title", "Snippet"));
temp2++;
}
mItemList.add(new OverlayItem(p, "Title", "Snippet"));

// mItemList.add(new OverlayItem(q, "Title", "Snippet"));

populate();
}

@Override
protected OverlayItem createItem(int pIndex)
{
return mItemList.get(pIndex);
}


@Override
protected boolean onTap(int pIndex)
{
//EditText adress;
adress = (EditText) findViewById(R.id.simpleGM_adress); // Get address from XML
//String addressInput = adress.getText().toString(); // Get input text

message=zipcode[pIndex]+addressd[pIndex]+problemd[pIndex];
Toast.makeText(Mapping.this,message,Toast.LENGTH_SHORT).show();
return false;



}


@Override
public int size()
{
return mItemList.size();
}
}




@Override
protected boolean isRouteDisplayed()
{
return false;
}
}
Attachments
mapOverlays1.rar
(42.46 KiB) Downloaded 106 times
Sai
saisri2k2
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Oct 29, 2008 7:57 pm
Location: Houston

Top

Postby saisri2k2 » Wed Nov 19, 2008 9:44 am

the code can be tested using the following zipcodes

77058
77057
77044
77042
77568

thanks
Sai
saisri2k2
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Oct 29, 2008 7:57 pm
Location: Houston

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 5 guests