2012-11-12 30 views
1

我想知道如何將各種不同的圖標添加到Google地圖?目前我可以添加一個,但是想要添加從列表中選擇的不同的列表或放置在地圖上的任何東西。如何將圖標更改爲Google地圖中的不同圖標

這裏是我的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

編輯:我在您的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); 

就是這樣的。我在解釋事情上真的很糟糕,但我希望這是有道理的。

+0

謝謝你的回答。但是對於地圖來說,我是完全陌生的,有點模糊地知道你在說什麼,但是有一些我可以遵循的教程會對我有更清晰的描述嗎? – Allrounder

+0

在閱讀完代碼後,編輯了我的答案。 – user1810737

+0

謝謝。這是完全有道理的,但是我不確定你在哪裏放置你建議的第一個代碼。 – Allrounder

0

您可以使用此庫 - Android MapView Balloons。此項目提供了一種簡單的方法,可以在使用Android地圖時使用簡單的信息框註釋地圖疊加項目。

相關問題