【问题标题】:Multiple line or break line in .snippet (google maps apiv2).snippet 中的多行或换行(谷歌地图 apiv2)
【发布时间】:2013-06-27 19:19:37
【问题描述】:

我正在使用谷歌地图 APIv2 进行开发。我现在面临的问题是我只能在 info windows/sn-p 中添加一行单词。但是我想要的输出能够以折线形式显示,如下面的示例所示。那么有没有可能的方法来解决呢?

例子:

坐标:03.05085, 101.70506

限速:80 公里/小时

public class MainActivity extends FragmentActivity {
  static final LatLng SgBesi = new LatLng(03.05085, 101.76022);
  static final LatLng JB = new LatLng(1.48322, 103.69065);
  private GoogleMap map;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    map.addMarker(new MarkerOptions().position(SgBesi)
        .title("KM D7.7 Sungai Besi")               //name
        .snippet("Coordinate: 03.05085, 101.70506"));   //description

    map.addMarker(new MarkerOptions().position(JB)
        .title("test") 
        .snippet("Hey, how are you?")); 

       // .icon(BitmapDescriptorFactory  //set icon
       //     .fromResource(R.drawable.ic_launcher)));

    // move the camera instantly with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(SgBesi, 15));

    // zoom in, animating the camera.       
    map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
   }
}

【问题讨论】:

  • 在我经历了与上面提供的代码类似的例子之后,我仍然无法解决我的问题。我不擅长编程,也不太了解如何使用自定义信息窗口指定特定位置。我测试过的大多数示例都没有指定特定位置。任何人都可以发布完整的编码供我参考吗?如果有人可以帮助我解决我已经测试了 3 天的最后一年项目问题的一部分,我将不胜感激。非常感谢。

标签: google-maps google-maps-android-api-2 google-maps-api-2 infowindow


【解决方案1】:

您需要使用custom InfoWindowAdapterInfoWindow 的布局更改为使用布局文件定义的自定义设计。基本上你需要:

  1. 使用GoogleMap.setInfoWindowAdapter(Yourcustominfowindowadpater) 声明您的函数
  2. 有一个像下面这样的课程:

...

class Yourcustominfowindowadpater implements InfoWindowAdapter {
    private final View mymarkerview;

    Yourcustominfowindowadpater() {
        mymarkerview = getLayoutInflater()
            .inflate(R.layout.custominfowindow, null); 
    }

    public View getInfoWindow(Marker marker) {      
        render(marker, mymarkerview);
        return mymarkerview;
    }

    public View getInfoContents(Marker marker) {
       return null;
    }

    private void render(Marker marker, View view) {
       // Add the code to set the required values 
       // for each element in your custominfowindow layout file
    }
}

【讨论】:

  • @PSIXO:不,按钮不起作用。来自the docs:“信息窗口......不是实时视图......不会尊重......触摸或手势事件。但是您可以在整个信息窗口上收听通用点击事件......” .所以你可以使用“整个信息窗口事件”来监听按钮上的点击,但按钮本身只是图像上的一些像素(所以也没有按钮点击动画)。
【解决方案2】:

您必须使用 InfoWindowAdapter 来创建自定义信息窗口。默认实现以某种方式从 sn-p 中删除换行符,这可能被认为是一个小错误。

【讨论】:

    【解决方案3】:

    我想向@tony 添加一些内容,根据documentation,您无法在自定义视图中添加操作,因为它在运行时绘制为图像。

    注意:绘制的信息窗口不是实时视图。观点是 渲染为图像(使用 View.draw(Canvas)) 回来。这意味着对视图的任何后续更改都不会 由地图上的信息窗口反映。更新信息窗口 稍后(例如,加载图像后),调用 showInfoWindow()。 此外,信息窗口不会尊重任何交互性 典型的普通视图,例如触摸或手势事件。然而你 可以在整个信息窗口上监听一个通用的点击事件 在下面的部分中描述。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多