Android Map v2标记泡泡

时间:2013-10-24 10:11:08

标签: android google-maps google-maps-markers google-maps-android-api-2 android-maps-v2

我对点击地图标记时弹出的气泡有疑问。我搜索过互联网,但没有真正令人满意的答案。基本上我的问题是:如果没有我单击它,有没有办法让泡泡出现在默认值上?

我在下面的代码中显示了这一部分:

MarkerOptions tousgårdsladen = new MarkerOptions()
    .position(TOUSGÅRDSLADEN).title("Activity Name")
    .snippet("Snippet");" 

如果单击标记,则会弹出一个带有“活动名称”文本的气泡,所以基本上我希望在没有单击的情况下加载标记时显示该气泡,如果单击它会为您提供有关活动的更多详细信息放置标记。

public class MapFragment extends Fragment {


Activity activity;
double lat;
double lng;
LatLng currentPosition;
SeekBar seekBar;
TextView textView;
GoogleMap map;

static final LatLng TOUSGÅRDSLADEN = new LatLng(56.16266, 10.13851);
static final LatLng DORTESVE = new LatLng(56.15952, 10.13570);
static final LatLng GELLERUP = new LatLng(56.154633, 10.133679);

private HashMap<Marker, Event> eventMarkerMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.e(tag, "inside create map_view");

    // Context
    activity = getActivity();
    View rootView = inflater.inflate(R.layout.map_layout, container, false);

    SetMap();
    SetCameraLocation();

    textView = (TextView) rootView.findViewById(R.id.slide_bar_text);
    seekBar = (SeekBar) rootView.findViewById(R.id.slide_bar);
    SetSeekBar();

    return rootView;
}

private void SetCameraLocation() {
    LocationManager locationManager = (LocationManager) activity
            .getSystemService(activity.LOCATION_SERVICE);
    Location location = locationManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location != null) {
        currentPosition = new LatLng(location.getLatitude(),
                location.getLongitude());
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPosition,
                14));
    } else {
        // default is setted to Gellerup
        currentPosition = new LatLng(56.154633, 10.133679);
    }
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 14));

}

private void SetMap() {
    map = ((SupportMapFragment) getFragmentManager().findFragmentById(
            R.id.map)).getMap();

    if (map != null) {
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        map.setMyLocationEnabled(true);
        // map.getUiSettings().setZoomControlsEnabled(false);

        MarkerOptions tousgårdsladen = new MarkerOptions()
                .position(TOUSGÅRDSLADEN).title("Activity Name")
                .snippet("Snippet");
        ;
        MarkerOptions dortesve = new MarkerOptions().position(DORTESVE)
                .title("Activity Name").snippet("Snippet");
        map.addMarker(tousgårdsladen);
        map.addMarker(dortesve);
    }
}

private void SetSeekBar() {
    seekBar.setMax(2); // Three options
    seekBar.incrementProgressBy(1);
    seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            int kilometers = 0;
            switch (progress) {
            case 0:
                kilometers = 2;
                break;
            case 1:
                kilometers = 4;
                break;
            case 2:
                kilometers = 6;
                break;
            }

            textView.setText(kilometers + " km");

            LoadTrack loadTrack = new LoadTrack();
            List<Location> list = loadTrack.decodeGPX(activity);

            PolylineOptions line = new PolylineOptions();
            line.width(5);
            line.color(R.color.palegreen);

            for (Location location : list) {
                LatLng latLng = new LatLng(location.getLatitude(), location
                        .getLongitude());
                line.add(latLng);
            }

            map.addPolyline(line);

        }
    });

}

}

       <?xml version="1.0" encoding="utf-8"?>

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" >


 <fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/slide_bar_text"
    />

 <SeekBar android:id="@+id/slide_bar"
     android:layout_alignParentBottom="true"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:background="@color/palegreen"


      />
 <TextView android:id="@+id/slide_bar_text"
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:layout_centerHorizontal="true"
     android:layout_above="@id/slide_bar"
     android:text="4"/>

1 个答案:

答案 0 :(得分:0)

这很容易。 使用以下代码:

Marker marker = myMap.addMarker(new MarkerOptions()
                 .position(latLng)
                 .title("Title")
                 .snippet("Snippet")
                 .icon(BitmapDescriptorFactory
                 .fromResource(R.drawable.marker)));

marker.showInfoWindow();