我想知道如何为谷歌地图添加各种不同的图标?目前我可以添加一个,但想要添加从列表中选择的不同或者放在地图上的任何内容。
这是我的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();
}
}
如果有人可以指出我正确的方向或者有一个关于锄头的教程,那将会非常有帮助,因为我从这一点开始就完全没有了! 感谢。
答案 0 :(得分: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);
这样的事情。我很擅长解释事情,但我希望这是有道理的。
答案 1 :(得分:0)
您可以使用此库 - Android MapView Balloons。使用Android Maps时,此项目提供了一种使用简单信息气球注释地图叠加项目的简便方法。