2017-12-22 5 views
-2

지도에서 샘플을 만들고 있는데이 곳에서 텍스트와 함께 표시자를 표시하려고합니다.codameone의지도에 텍스트로 표시자를 표시하는 방법

그것에 대해 내가 코드를 아래에 사용.

MapContainer mapContainer = new MapContainer("xxxxxxxxxxxxxxxxxxxxxx"); 
Coord coord = new Coord(latitude,longitude); 
mapContainer.setCameraPosition(coord); 
mapContainer.addMarker(EncodedImage.createFromImage(image,false), mapContainer.getCameraPosition(), "Text", "Text", null); 

그러나이 코드는 마커를 표시하는 데 도움이되지만 텍스트와 함께 표시하지는 않습니다. 그래서 누군가가 텍스트로 마커를 표시 할 생각이라면/이것을 성취하도록 도와주세요.

미리 감사드립니다. 여기

는 MapContainer 및 (대답 아래) MapLayout를 사용하여 코드를입니다 ..

if(BrowserComponent.isNativeBrowserSupported()) { 
     MapContainer mc = new MapContainer("AIzaSyDLIu4RfdXVQPvRqOYLP6N8ocCQpPNqtIk"); 
     mapDemo.add(mc); 
     Container markers = new Container(); 
     markers.setLayout(new MapLayout(mc, markers)); 
     mapDemo.add(markers); 

     Coord moscone = new Coord(37.7831, -122.401558); 
     Button mosconeButton = new Button(""); 
     FontImage.setMaterialIcon(mosconeButton, FontImage.MATERIAL_PLACE); 
     markers.add(moscone, mosconeButton); 

     Coord moscone1 = new Coord(36.6139, -120.2090); 
     Button mosconeButton1 = new Button(""); 
     FontImage.setMaterialIcon(mosconeButton1, FontImage.MATERIAL_PLACE); 
     markers.add(moscone1, mosconeButton1); 

     mc.zoom(moscone1, 5); 
    } else { 
     // iOS Screenshot process... 
     mapDemo.add(new Label("Loading, please wait....")); 
    } 
+0

https://stackoverflow.com/help/how-to-ask – dda

답변

1

이 개정 된 대답은 아래 참조를 위해 원래의 대답을 볼 수 있습니다 :이 더 잘 작동합니다 새 업데이트로

이전 버전 인 경우 Google지도 용 cn1lib도 업데이트해야합니다. 우리는 또한 다시 MapLayout 클래스를 수정 및 정렬과 같은 몇 가지 기능이 추가되었습니다 :

public class MapLayout extends Layout implements MapListener { 
     private static final String COORD_KEY = "$coord"; 
     private static final String POINT_KEY = "$point"; 
     private static final String HORIZONTAL_ALIGNMENT = "$align"; 
     private static final String VERTICAL_ALIGNMENT = "$valign"; 
     private final MapContainer map; 
     private final Container actual; 
     private boolean inUpdate; 
     private Runnable nextUpdate;   
     private int updateCounter; 

     public static enum HALIGN { 
      LEFT { 
       @Override 
       int convert(int x, int width) { 
        return x; 
       } 
      }, 
      CENTER { 
       @Override 
       int convert(int x, int width) { 
        return x - width/2; 
       } 
      }, 
      RIGHT { 
       @Override 
       int convert(int x, int width) { 
        return x - width; 
       } 
      }; 

      abstract int convert(int x, int width); 
     } 

     public static enum VALIGN { 
      TOP { 
       @Override 
       int convert(int y, int height) { 
        return y; 
       } 
      }, 
      MIDDLE { 
       @Override 
       int convert(int y, int height) { 
        return y + height/2; 
       } 
      }, 
      BOTTOM { 
       @Override 
       int convert(int y, int height) { 
        return y + height; 
       } 
      }; 

      abstract int convert(int y, int height); 
     } 

     public MapLayout(MapContainer map, Container actual) { 
      this.map = map; 
      this.actual = actual; 
      map.addMapListener(this); 
     } 

     @Override 
     public void addLayoutComponent(Object value, Component comp, Container c) { 
      comp.putClientProperty(COORD_KEY, (Coord) value); 
     } 

     @Override 
     public boolean isConstraintTracking() { 
      return true; 
     } 

     @Override 
     public Object getComponentConstraint(Component comp) { 
      return comp.getClientProperty(COORD_KEY); 
     } 

     @Override 
     public boolean isOverlapSupported() { 
      return true; 
     } 

     public static void setHorizontalAlignment(Component cmp, HALIGN a) { 
      cmp.putClientProperty(HORIZONTAL_ALIGNMENT, a); 
     } 

     public static void setVerticalAlignment(Component cmp, VALIGN a) { 
      cmp.putClientProperty(VERTICAL_ALIGNMENT, a); 
     } 

     @Override 
     public void layoutContainer(Container parent) { 
      int parentX = 0; 
      int parentY = 0; 
      for (Component current : parent) { 
       Coord crd = (Coord) current.getClientProperty(COORD_KEY); 
       Point p = (Point) current.getClientProperty(POINT_KEY); 
       if (p == null) { 
        p = map.getScreenCoordinate(crd); 
        current.putClientProperty(POINT_KEY, p); 
       } 
       HALIGN h = (HALIGN)current.getClientProperty(HORIZONTAL_ALIGNMENT); 
       if(h == null) { 
        h = HALIGN.LEFT; 
       } 
       VALIGN v = (VALIGN)current.getClientProperty(VERTICAL_ALIGNMENT); 
       if(v == null) { 
        v = VALIGN.TOP; 
       } 
       current.setSize(current.getPreferredSize()); 
       current.setX(h.convert(p.getX() - parentX, current.getWidth())); 
       current.setY(v.convert(p.getY() - parentY, current.getHeight())); 
      } 
     } 

     @Override 
     public Dimension getPreferredSize(Container parent) { 
      return new Dimension(100, 100); 
     } 

     @Override 
     public void mapPositionUpdated(Component source, int zoom, Coord center) { 
      Runnable r = new Runnable() { 
       public void run() { 
        inUpdate = true; 
        try { 
         List<Coord> coords = new ArrayList<>(); 
         List<Component> cmps = new ArrayList<>(); 
         int len = actual.getComponentCount(); 
         for (Component current : actual) { 
          Coord crd = (Coord) current.getClientProperty(COORD_KEY); 
          coords.add(crd); 
          cmps.add(current); 
         } 
         int startingUpdateCounter = ++updateCounter; 
         List<Point> points = map.getScreenCoordinates(coords); 
         if (startingUpdateCounter != updateCounter || len != points.size()) { 
          // Another update must have run while we were waiting for the bounding box. 
          // in which case, that update would be more recent than this one. 
          return; 
         } 
         for (int i=0; i<len; i++) { 
          Component current = cmps.get(i); 
          Point p = points.get(i); 
          current.putClientProperty(POINT_KEY, p); 
         } 
         actual.setShouldCalcPreferredSize(true); 
         actual.revalidate(); 
         if (nextUpdate != null) { 
          Runnable nex = nextUpdate; 
          nextUpdate = null; 
          callSerially(nex); 
         } 
        } finally { 
         inUpdate = false; 
        } 

       } 

      }; 
      if (inUpdate) { 
       nextUpdate = r; 
      } else { 
       nextUpdate = null; 
       callSerially(r); 
      }   
     } 
} 

원래 답변 : 당신이 그것에 누를 때

마커는 텍스트를 표시됩니다. 상황이 다르게 나타나길 원하면 모든 구성 요소를 사용하여지도 상단에 배치하면됩니다. 곧 Uber tutorial 깊이에서이 문제를 다룰 것입니다하지만 난이 블로그 게시물에서 기본 시스템을 설명했다 https://www.codenameone.com/blog/tip-map-layout-manager.html

+0

의 작업 만 축소하는 동안 나는 여러 마커를 추가하는 경우 (in/out) 몇 시간 마커가 서로 겹쳐지고 stackoverflow 오류가 발생합니다 .. 그리고 lts는 시뮬레이터의 장치에서만 발생합니다. – tyson

+0

많은 마커가있는 경우 겹칩니다. 맵 리스너를 사용하여 마커를 추가/제거하고 통합 마커 효과를 만들 수 있습니다. 이러한 모든 문제를 해결할 표준화 된 레이아웃 API를 개발하고자 할 수 있습니다. 예외가 표시되는 경우 USB 케이블을 사용하여 장치 출력물을 검사하고 스택의 뿌리가 넘치는지 확인하십시오. –

+0

지도에 마커가 두 개 이상 추가 된 경우지도 이동/확대/축소 표시가 안정적이지 않고 같은 위치에 표시되고 계속 깜박입니다. – tyson

관련 문제