好吧,我现在一整天都在这,我放弃试图自己做到这一点!
我已经宣布了地图和位置
private GoogleMap googleMap;
宣布了这个位置
static final LatLng SECC = new LatLng(55.8607,-4.2871);
生成地图
googleMap=((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
建立了标记
Marker secc =googleMap.addMarker(new MarkerOptions().position(SECC) .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)) .title("SECC").snippet("Exhibition Way, Glasgow, G3 8YW\nSports: Boxing, Gymnastics, Judo, Netball, Wrestling, Weightlifting"));
将点击侦听器设置为地图
googleMap.setOnMarkerClickListener((OnMarkerClickListener)this);
设置自定义信息框
googleMap.setInfoWindowAdapter(new InfoWindowAdapter() { @Override public View getInfoWindow(Marker arg0) { return null; } public View getInfoContents(Marker marker) { View v = getLayoutInflater().inflate(R.layout.marker,null); TextView info=(TextView) v.findViewById(R.id.info); info.setText("hello"); return v; } });
并设置click事件
googleMap.setOnMarkerClickListener(new OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { marker.showInfoWindow(); return true; } });
谁能指出我哪里出错?地图加载,您可以单击标记以获取代码段,但不显示自定义信息
编辑以包含清单
以下代码可能对您有所帮助.
公共类Stacky扩展FragmentActivity实现OnMarkerClickListener {
静态最终LatLng SECC =新LatLng(55.8607,-4.2871); 私人GoogleMap mMap;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.basic_demo); mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map2)).getMap(); mMap.setMyLocationEnabled(true); setUpMap(); findSMSLocation(); // Setting a custom info window adapter for the google map mMap.setInfoWindowAdapter(new InfoWindowAdapter() { // Use default InfoWindow frame @Override public View getInfoWindow(Marker marker) { return null; } // Defines the contents of the InfoWindow @Override public View getInfoContents(Marker marker) { // Getting view from the layout file info_window_layout View v = getLayoutInflater().inflate(R.layout.custom_info, null); TextView tvLat = (TextView) v.findViewById(R.id.info); tvLat.setText("info"); tvLat.setTextColor(Color.GREEN); return v; } }); } @Override protected void onPause() { super.onPause(); } @Override protected void onResume() { super.onResume(); setUpMapIfNeeded(); } public void findSMSLocation() { // TODO Auto-generated method stub try{ mMap.addMarker(new MarkerOptions().position(SECC).title("SECC").snippet("Exhibition Way, Glasgow, G3 8YW\nSports: Boxing, Gymnastics, Judo, Netball, Wrestling, Weightlifting")); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(SECC, 18.0f)); }catch(Exception e){ e.printStackTrace(); } } private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (mMap == null) { // Try to obtain the map from the SupportMapFragment. mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map2)) .getMap(); mMap.setMyLocationEnabled(true); // Check if we were successful in obtaining the map. if (mMap != null) { setUpMap(); } } } private void setUpMap() { mMap.getUiSettings().setCompassEnabled(true); mMap.getUiSettings().setTiltGesturesEnabled(true); mMap.getUiSettings().setRotateGesturesEnabled(true); mMap.getUiSettings().setScrollGesturesEnabled(true); mMap.getUiSettings().setZoomControlsEnabled(true); mMap.getUiSettings().setZoomGesturesEnabled(true); } @Override public boolean onMarkerClick(Marker marker) { // TODO Auto-generated method stu return false; }
}
您不需要将marker.showInfoWindow()添加到OnMarkerClickListner()接口.当您单击n到标记时,然后创建自定义窗口并使用您的自定义textview加载.