开发者

gwt removing a marker

开发者 https://www.devze.com 2023-02-05 16:16 出处:网络
I have a problem. here\'s my code: map.addMapClickHandler(new MapClickHandler() { public void onClick(MapClickEvent e)

I have a problem. here's my code:

 map.addMapClickHandler(new MapClickHandler() 
     {
      public void onClick(MapClickEvent e) 
      {
        MapWidget sender = e.getSender();
           Overlay overlay = e.getOverlay();
           LatLng pkt = e.getLatLng();
       if (overlay != null && overlay instanceof Marker) 
       {
       // double a = pkt.getLatitude();
        //double b = pkt.getLongitude();
       // String trr = a+";"+b;
        sender.removeOverlay(overlay);
       } 

       else 
       {
        sender.addOverlay(new Marker(pkt));
        double a = pkt.getLatitude();
        double b = pkt.getLongitude();
        point[akt]=a+";"+b;
          }
         });

and the question is why can't I get Latitude and Longitude when overlay isn't null? (the comented area) all the time there is alert that "uncaught exception escaped". how to do it right?


no, after more tests, it isn't working :/

because in

if (overlay != null && overlay instanceof Marker) {
  LatL开发者_Go百科ng pkt = e.getOverlayLatLng();
  double a = pkt.getLatitude();
  double b = pkt.getLongitude();
  String trr = a + ";" + b;
  sender.removeOverlay(overlay);
}

Longitude and Latitude I get is the one of the last Marker I added (no matter which Marker I click on). is it possible to get the right values?


The JavaDocs for MapClickEvent indicate that MapClickEvent#getOverlayLatLng() should be used when the user has clicked on an overlay (note that I have not tested this code):

map.addMapClickHandler(new MapClickHandler(){
  public void onClick(MapClickEvent e) {
    MapWidget sender = e.getSender();
    Overlay overlay = e.getOverlay();

    if (overlay != null && overlay instanceof Marker) {
      LatLng pkt = e.getOverlayLatLng();
      double a = pkt.getLatitude();
      double b = pkt.getLongitude();
      String trr = a + ";" + b;
      sender.removeOverlay(overlay);
    } else if (overlay == null) {
      LatLng pkt = e.getLatLng();
      sender.addOverlay(new Marker(pkt));
      double a = pkt.getLatitude();
      double b = pkt.getLongitude();
      point[akt] = a + ";" + b;
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消