android如何在地图v2上制作一个可拖动的引脚

时间:2013-10-10 22:40:15

标签: android google-maps location

大家好,我正试图制作一个别针..问题是我无法找到与我的问题相关的来源。我已经检查了commonsware主题,但没有一个包含可拖动的引脚。实际上其中一个有,但它与地图v1而不是v2有关。那么如何在用户当前位置制作一个引脚,然后当它们点击引脚时可以在地图上的任何位置拖动..

这是一个示例代码,它有一个pin(标记)并找到用户位置..

public class MainActivity extends AbstractMapActivity implements
OnNavigationListener, OnInfoWindowClickListener,
OnMyLocationChangeListener {
private static final String STATE_NAV="nav";
private static final int[] MAP_TYPE_NAMES= { R.string.normal,
  R.string.hybrid, R.string.satellite, R.string.terrain };
private static final int[] MAP_TYPES= { GoogleMap.MAP_TYPE_NORMAL,
  GoogleMap.MAP_TYPE_HYBRID, GoogleMap.MAP_TYPE_SATELLITE,
  GoogleMap.MAP_TYPE_TERRAIN };
private GoogleMap map=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (readyToGo()) {
  setContentView(R.layout.activity_main);

  SupportMapFragment mapFrag=
      (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

  initListNav();

  map=mapFrag.getMap();

  if (savedInstanceState == null) {
    CameraUpdate center=
        CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
                                                 -73.98180484771729));
    CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

    map.moveCamera(center);
    map.animateCamera(zoom);
  }

  addMarker(map, 40.748963847316034, -73.96807193756104,
            R.string.un, R.string.united_nations);
  addMarker(map, 40.76866299974387, -73.98268461227417,
            R.string.lincoln_center,
            R.string.lincoln_center_snippet);
  addMarker(map, 40.765136435316755, -73.97989511489868,
            R.string.carnegie_hall, R.string.practice_x3);
  addMarker(map, 40.70686417491799, -74.01572942733765,
            R.string.downtown_club, R.string.heisman_trophy);

  map.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
  map.setOnInfoWindowClickListener(this);

  map.setMyLocationEnabled(true);
  map.setOnMyLocationChangeListener(this);
 }
}

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
map.setMapType(MAP_TYPES[itemPosition]);

return(true);
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);

savedInstanceState.putInt(STATE_NAV,
                          getSupportActionBar().getSelectedNavigationIndex());
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);

getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_NAV));
}

@Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(this, marker.getTitle(), Toast.LENGTH_LONG).show();
}

@Override
public void onMyLocationChange(Location lastKnownLocation) {
Log.d(getClass().getSimpleName(),
      String.format("%f:%f", lastKnownLocation.getLatitude(),
                    lastKnownLocation.getLongitude()));
}

private void initListNav() {
ArrayList<String> items=new ArrayList<String>();
ArrayAdapter<String> nav=null;
ActionBar bar=getSupportActionBar();

for (int type : MAP_TYPE_NAMES) {
  items.add(getString(type));
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
  nav=
      new ArrayAdapter<String>(
                               bar.getThemedContext(),
                               android.R.layout.simple_spinner_item,
                               items);
}
else {
  nav=
      new ArrayAdapter<String>(
                               this,
                               android.R.layout.simple_spinner_item,
                               items);
}

nav.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
bar.setListNavigationCallbacks(nav, this);
}

private void addMarker(GoogleMap map, double lat, double lon,
                     int title, int snippet) {
map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
                                 .title(getString(title))
                                 .snippet(getString(snippet)));
  }
}

1 个答案:

答案 0 :(得分:2)

您需要的所有文档就在这里

https://developers.google.com/maps/documentation/android/

您只需将标记设置为可拖动

即可
marker.setDraggable(true);