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();
}
}
誰かが私を正しい方向に向けたり、鍬のチュートリアルを持っていたりすることができれば、それは本当に役に立ちます。私はこの時点から完全に切り株です! ありがとう。