Iam displaying multiple pins in asynctask from the raw folder..on single tap listener comparig the tapped listener lat long values with the dropped pin lat longs..
private class LocationsDisplaying extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
InputStream is = MainActivity.this.getResources()
.openRawResource(R.raw.service);
xr.parse(new InputSource(is));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
new PinDisplay().execute();
}
}
类 PinDisplay 扩展 AsyncTask { ProgressDialog 对话框;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("Please wait...");
dialog.setCanceledOnTouchOutside(false);
dialog.setMessage("Loading......");
dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
displaying_mapUI();
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
}
}
private void displaying_mapUI() {
// TODO Auto-generated method stub
Point latlong = null;
Point point;
Graphic graphic;
GraphicsLayer gLayer;
if (MyXMLHandler.getResponse().getLatitude().size() > 0) {
for (int i = 0; i < MyXMLHandler.getResponse().getLatitude().size(); i++) {
Log.v("TEST", "size=="
+ MyXMLHandler.getResponse().getLatitude().size());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
double latitude = Double.parseDouble(MyXMLHandler.getResponse()
.getLatitude().get(i).toString());
double langtude = Double.parseDouble(MyXMLHandler.getResponse()
.getLongtude().get(i).toString());
Log.v("TEST", "lan........" + latitude + "....lng......"
+ langtude);
latlong = new Point(langtude, latitude);
point = (Point) GeometryEngine.project(latlong,
SpatialReference.create(4326),
mMapView.getSpatialReference());
gLayer = new GraphicsLayer();
PictureMarkerSymbol pictureMarkerSymbol = new PictureMarkerSymbol(
getResources().getDrawable(R.drawable.pushpin2));
graphic = new Graphic(point, pictureMarkerSymbol);
gLayer.addGraphic(graphic);
mMapView.addLayer(gLayer);
}
}
}
@Override
public void onSingleTap(final float x, final float y) {
// TODO Auto-generated method stub
mMapView.setRotationAngle(0);
compass.setRotationAngle(mMapView.getRotationAngle());
compass.postInvalidate();
LayoutInflater inflator = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflator.inflate(R.layout.callout_xml, null);
TextView text = (TextView) view.findViewById(R.id.textView1);
// View view = createView();
Point p = mMapView.toMapPoint(x, y);
// Log.v("TEST", "X=" + p.getX());
// Log.v("TEST", "Y=" + p.getY());
SpatialReference sp = SpatialReference.create(4326);
Point aux = (Point) GeometryEngine.project(p,
mMapView.getSpatialReference(), sp);
location = new Point(aux.getX(), aux.getY());
StringTokenizer tokenst1 = new StringTokenizer(String.valueOf(aux
.getY()), ".");
String firstt1 = tokenst1.nextToken();
float tapLat = Float.parseFloat(firstt1);
StringTokenizer tokenst2 = new StringTokenizer(String.valueOf(aux
.getX()), ".");
String firstt2 = tokenst2.nextToken();
float tapLng = Float.parseFloat(firstt2);
StringTokenizer tokensl1 = new StringTokenizer(String.valueOf(latlong
.getY()), ".");
String firstl1 = tokensl1.nextToken();
float staticLat = Float.parseFloat(firstl1);
StringTokenizer tokensl2 = new StringTokenizer(String.valueOf(latlong
.getX()), ".");
String firstl2 = tokensl2.nextToken();
float staticLng = Float.parseFloat(firstl2);
Log.v("TEST", "tapLat" + tapLat);
Log.v("TEST", "tapLng" + tapLng);
Log.v("TEST", "staticLat" + staticLat);
Log.v("TEST", "staticLng" + staticLng);
if (tapLat == staticLat && tapLng == staticLng) {
locationName = "Alberta";
} else {
for (int j = 0; j < MyXMLHandler.getResponse().getLatitude().size(); j++) {
double double_lat = MyXMLHandler.getResponse().getLatitude().get(j);
double double_long = MyXMLHandler.getResponse().getLongtude().get(j);
StringTokenizer tokens = new StringTokenizer(String.valueOf(double_lat),
".");
String first = tokens.nextToken();
Log.v("TEST", "first......" + first);
StringTokenizer tokens1 = new StringTokenizer(
String.valueOf(double_long), ".");
String first1 = tokens1.nextToken();
Log.v("TEST", "first1111111......" + first1);
float service_location_lat = Float.parseFloat(first);
float service_location_lng = Float.parseFloat(first1);
if (tapLat == service_location_lat
&& tapLng == service_location_lng) {
Log.v("TEST", "if service_location_lng....."
+ service_location_lng);
Log.v("TEST", "if service_location_lat........"
+ service_location_lat);
locationName = MyXMLHandler.getResponse().getName().get(j)
.toString().trim();
Log.v("TESt", "locationName....." + locationName);
j = MyXMLHandler.getResponse().getLatitude().size();
} else {
locationName = "No Results Found";
}
}
}
text.setText(locationName);
callout.show(p, view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (locationName.equalsIgnoreCase("No Results Found")
|| locationName.equalsIgnoreCase("Alberta")) {
Toast.makeText(getApplicationContext(),
"Please select Location", Toast.LENGTH_SHORT)
.show();
} else {
Intent intent = new Intent(MainActivity.this,
RouteDisplying.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
});
}