1

我想知道如何将各种不同的图标添加到谷歌地图?目前我可以添加一个,但想添加从列表中选择的不同的或放置在地图上的任何内容。

这是我的 MainActivity 代码:

 public class MainActivity extends MapActivity implements LocationListener {

 MapView map;
 long start;
 long stop;
 MyLocationOverlay compass;
 MapController controller;
 int x, y;
 GeoPoint touchedPoint;
 Drawable d;
 List<Overlay> overlayList;
 LocationManager lm;
 String towers;
 int lat;
 int longi;

@Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 map = (MapView) findViewById(R.id.mapview);
 map.setBuiltInZoomControls(true);

 Touchy t = new Touchy();
 overlayList = map.getOverlays();
 overlayList.add(t);
 compass = new MyLocationOverlay(MainActivity.this, map);
 overlayList.add(compass);
 controller = map.getController();
 GeoPoint point = new GeoPoint((int)(-24.0110 * 1E6), (int)(31.4850 * 1E6));
 controller.animateTo(point);
 controller.setZoom(10);
 d = getResources().getDrawable(R.drawable.icon);

 //Placing PinPoint at location
 lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
 Criteria crit = new Criteria();

 towers = lm.getBestProvider(crit, false);
 Location location = lm.getLastKnownLocation(towers);

 if (location != null){
     lat = (int) (location.getLatitude() *1e6);
     longi = (int) (location.getLongitude() *1E6);
     GeoPoint ourLocation = new GeoPoint(lat, longi);
     OverlayItem overlayItem = new OverlayItem(ourLocation, "What's Up", "2nd String");
        CustomPinPoint custom = new CustomPinPoint(d, MainActivity.this);
        custom.insertPinpoint(overlayItem);
        overlayList.add(custom);
 }else{
     Toast.makeText(MainActivity.this, "Couldn't Get Provider", Toast.LENGTH_SHORT).show();
 }


}


  @Override
protected void onPause() {
    // TODO Auto-generated method stub
    compass.disableCompass();
    super.onPause();
    lm.removeUpdates(this);
}


@Override
protected void onResume() {
    compass.enableCompass();
    // TODO Auto-generated method stub
    super.onResume();
    lm.requestLocationUpdates(towers, 500, 1, this);
}


 @Override
 protected boolean isRouteDisplayed() {
 return false;
 }
class Touchy extends Overlay{
public boolean onTouchEvent(MotionEvent e, MapView m){
        if (e.getAction() == MotionEvent.ACTION_DOWN){
            start = e.getEventTime();
            x = (int) e.getX();
            y = (int) e.getY();
            touchedPoint = map.getProjection().fromPixels(x, y);
        }
        if (e.getAction() == MotionEvent.ACTION_UP){
            stop = e.getEventTime();
        }
        if (stop - start > 1500){
            AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
            alert.setTitle("Pick an Option");
            alert.setMessage("Option has been Picked");
            alert.setButton(DialogInterface.BUTTON_POSITIVE, "Place a pinpoint", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                //TODO Auto=generated method stub

                OverlayItem overlayItem = new OverlayItem(touchedPoint, "What's Up", "2nd String");
                CustomPinPoint custom = new CustomPinPoint(d, MainActivity.this);
                custom.insertPinpoint(overlayItem);
                overlayList.add(custom);
            }

            });
            alert.setButton(DialogInterface.BUTTON_NEUTRAL, "Address", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which)
                {
                    //TODO Auto=generated method stub
                    Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault());
                    try{
                        List<Address> address = geocoder.getFromLocation(touchedPoint.getLatitudeE6() / 1E6, touchedPoint.getLongitudeE6() / 1E6, 1);
                        if (address.size() > 0){
                            String display = "";
                            for (int i = 0; i<address.get(0).getMaxAddressLineIndex(); i++){

                                display += address.get(0).getAddressLine(i) + "\n";
                            }
                        Toast t = Toast.makeText(getBaseContext(), display, Toast.LENGTH_LONG);
                        t.show();
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }finally{

                    }
                    }
                    });
                    alert.setButton(DialogInterface.BUTTON_NEGATIVE, "Toggle View", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    //TODO Auto=generated method stub
                    if (map.isSatellite()){
                        map.setSatellite(false);
                        }else{
                            map.setSatellite(true);
                        }
                }


                });
            alert.show();
            {
        return true;
        }
        }
        return false;
}
}
public void onLocationChanged(Location l) {
    // TODO Auto-generated method stub
    lat = (int) (l.getLatitude() *1E6);
    longi = (int) (l.getLongitude() *1E6);
    GeoPoint ourLocation = new GeoPoint(lat, longi);
    OverlayItem overlayItem = new OverlayItem(ourLocation, "What's Up", "2nd String");
    CustomPinPoint custom = new CustomPinPoint(d, MainActivity.this);
    custom.insertPinpoint(overlayItem);
    overlayList.add(custom);
}


public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}


public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}


public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}
  }

对于我的 CustomPinPoint 类:

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class CustomPinPoint extends ItemizedOverlay<OverlayItem>{

private ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();
private Context c;

public CustomPinPoint(Drawable defaultMarker) {
    super(boundCenter(defaultMarker));
    // TODO Auto-generated constructor stub
}
public CustomPinPoint(Drawable m, Context context) {
    this(m);
    c = context;
    // TODO Auto-generated constructor stub
}
@Override
protected OverlayItem createItem(int i) {
    // TODO Auto-generated method stub
    return pinpoints.get(i);
}

@Override
public int size() {
    // TODO Auto-generated method stub
    return pinpoints.size();
}
public void insertPinpoint(OverlayItem item){
    pinpoints.add(item);
    this.populate();
}
}

如果有人能指出我正确的方向或有一个关于锄头的教程,那将非常有帮助,因为从这一点开始,我已经完全陷入困境!谢谢。

4

2 回答 2

0

编辑:我在您的 CustomPinPoint 类中看到您没有对 createItem 进行编程。从技术上讲,您的“CustomPinPoint”类是一个覆盖类;它是具有多个精确点的单层。您创建图层,并向该图层添加点。

用这个:

@Override
protected void createItem(Object o, Drawable d) {
OverlayItem i = new OverlayItem(<Geopoint location>,<name>,<title>);
i.setMarker(d);
pinpoints.add(i);  
}

这将使用自定义标记为您的图层添加一个精确点。添加完所有图标后,您应该立即填充()图层。

所以:

CustomPinPoint pinpoint = new CustomPinPoint();

[...] 在您的地图活动中

pinpoint.createItem(o, d); //Where o is the object info you want the marker to be about, and d is the custom drawable.

[...] 在您的地图活动中

map.getOverlays(pinpoint);

类似的东西。我真的不擅长解释事情,但我希望这是有道理的。

于 2012-11-12T21:40:15.137 回答
0

你可以使用这个库——Android MapView Balloons。该项目提供了一种在使用 Android 地图时使用简单信息气球注释地图叠加项目的简单方法。

于 2012-11-13T05:53:05.437 回答