我在数组列表中获得了附近地点的所有搜索结果,但我无法在地图上显示它们。
当我调试我的应用程序时,在SitesOverlay()
构造函数中添加断点时,这些位置会被填充,但如果我作为 android 应用程序运行则不会。
我的代码如下:
public class Map1 extends MapActivity {
private MapView map = null;
private MyLocationOverlay me = null;
double source_lat;
double source_lng;
double destination_lat = 12.899915;
double destination_lng = 80.243969;
Boolean isLocationFound = false;
List<Place> arlPlaces = new ArrayList<Place>();
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(new Runnable() {
public void run() {
System.out.println("@#@#@#@# main() @#@#@#@#");
String strRequestUrl = "";
try {
URL url = new URL(strRequestUrl);
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(yc.getInputStream()));
String inputLine;
Place place = null;
while ((inputLine = in.readLine()) != null) {
if (inputLine.contains("name")) {
place = new Place();
place.setStrName(inputLine.substring(
inputLine.indexOf(">") + 1,
inputLine.lastIndexOf("<")));
}
if (inputLine.contains("vicinity")) {
place.setStrAddress(inputLine.substring(
inputLine.indexOf(">") + 1,
inputLine.lastIndexOf("<")));
}
if (inputLine.contains("lat")) {
place.setDblLatitude(Double.valueOf(inputLine
.substring(inputLine.indexOf(">") + 1,
inputLine.lastIndexOf("<"))));
}
if (inputLine.contains("lng")) {
place.setDblLongitude(Double.valueOf(inputLine
.substring(inputLine.indexOf(">") + 1,
inputLine.lastIndexOf("<"))));
arlPlaces.add(place);
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
map = (MapView) findViewById(R.id.map);
map.getController().setCenter(getPoint(12.899915, 80.243969));
map.setStreetView(true);
map.setSatellite(false);
map.getController().setZoom(15);
map.setBuiltInZoomControls(true);
Drawable marker = getResources().getDrawable(R.drawable.marker);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
map.getOverlays().add(new SitesOverlay(marker));
me = new MyLocationOverlay(this, map);
map.getOverlays().add(me);
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
Button btnNearby = (Button) findViewById(R.id.button3);
btnNearby.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.button2:
if (map.isSatellite() == false) {
map.setSatellite(true);
}
break;
}
}
});
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
if (map.isSatellite() == true) {
map.setSatellite(false);
}
break;
}
}
});
}
@Override
protected boolean isRouteDisplayed() {
return (false);
}
private GeoPoint getPoint(double lat, double lon) {
return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
}
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items = new ArrayList<OverlayItem>();
public SitesOverlay(Drawable marker) {
super(marker);
boundCenterBottom(marker);
items.add(new OverlayItem(getPoint(12.899915, 80.243969), "",
"Mantri Signature Villas" + "\n" + "ECR Link Road, Chennai"));
Iterator<Place> iterator = arlPlaces.iterator();
while (iterator.hasNext()) {
Place place = (Place) iterator.next();
System.out.println("@#@$#@#$@#!#@$@$#@ iterator");
items.add(new OverlayItem(getPoint(place.getDblLatitude(),
place.getDblLongitude()),place.getStrName(), place.getStrAddress()));
}
populate();
}
@Override
protected OverlayItem createItem(int i) {
return (items.get(i));
}
@Override
protected boolean onTap(final int i) {
// Toast.makeText(A.this,
// items.get(i).getSnippet(),
// Toast.LENGTH_SHORT).show();
AlertDialog.Builder alert = new AlertDialog.Builder(Map1.this);
if (items
.get(i)
.getSnippet()
.equalsIgnoreCase(
"Mantri Signature Villas\nECR Link Road, Chennai")) {
alert.setTitle(items.get(i).getSnippet());
alert.setMessage("Would you like to find the Route?");
// alert.setIcon(R.drawable.mnlo);
alert.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location myLocation1 = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location myLocation = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (myLocation != null) {
String uri = "http://maps.google.com/maps?saddr="
+ (myLocation.getLatitude())
+ ","
+ (myLocation.getLongitude())
+ "&daddr="
+ destination_lat
+ ","
+ destination_lng;
Intent intent = new Intent(
android.content.Intent.ACTION_VIEW,
Uri.parse(uri));
intent.setClassName(
"com.google.android.apps.maps",
"com.google.android.maps.MapsActivity");
startActivity(intent);
} else if (myLocation1 != null) {
String uri = "http://maps.google.com/maps?saddr="
+ (myLocation1.getLatitude())
+ ","
+ (myLocation1.getLongitude())
+ "&daddr="
+ destination_lat
+ ","
+ destination_lng;
Intent intent = new Intent(
android.content.Intent.ACTION_VIEW,
Uri.parse(uri));
intent.setClassName(
"com.google.android.apps.maps",
"com.google.android.maps.MapsActivity");
startActivity(intent);
} else {
Intent intent = new Intent(
getBaseContext(), Map1.class);
startActivity(intent);
}
}
});
alert.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
alert.show();
} else {
alert.setTitle(items.get(i).getSnippet());
alert.setMessage("Would you like to find the Route?");
alert.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String uri = "http://maps.google.com/maps?saddr="
+ items.get(i).getPoint()
+ "&daddr="
+ destination_lat
+ ","
+ destination_lng;
Intent intent = new Intent(
android.content.Intent.ACTION_VIEW, Uri
.parse(uri));
intent.setClassName(
"com.google.android.apps.maps",
"com.google.android.maps.MapsActivity");
startActivity(intent);
}
});
alert.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
alert.show();
}
return (true);
}
public int size() {
return (items.size());
}
}
}