2011-12-24 1 views
0

블로거 중 한 명이 애플리케이션 내부에서 MapView 클래스를 호출하는 멋진 아이디어를 제공했습니다.레이아웃 내에서 MapView를 호출 할 수 있습니까?

turn by turn directions in MapView

코드

showDirections.setOnClickListener(new View.OnClickListener() { 

public void onClick(final View view) { final Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse( "http://maps.google.com/maps?" + "saddr=43.0054446,-87.9678884" + "&daddr=42.9257104,-88.0508355"));

  intent.setClassName(
      "com.google.android.apps.maps", 
      "com.google.android.maps.MapsActivity"); 
     startActivity(intent); 

}이다 });

이 Mapview를 레이아웃에 첨부하여 애플리케이션의 레이아웃에서 열 수 있습니까? 이 방법은 레이아웃과 응용 프로그램 외부에서 열립니다. 이 MapView를 레이아웃으로 수정하려고합니다. 모든 코드 예제. 회신을 기대합니다. 감사합니다.

+0

, UR 기초를 잘하고 몇 가지 조사를받을하시기 바랍니다는 다른 스택 오버플로 사용자를 위해 도움이되기를 바랍니다 –

답변

0

나는 내 자신의 질문에 대한 답을 쓰고 있고 다른 StackOverflow 사용자로부터 도움을 받았습니다.

소스 위도 및 경도에서 목적지 위도 및 경도로 방향 경로를 그려주는 전체 소스 코드입니다. GPS를 통해 위도와 경도에 액세스하는 모든 사용자는 자신의 GPS 장치에서 목적지 좌표로 방향을 가져올 수 있습니다.

StackOverflow에 대한 답변이 있었고 원본 및 대상 장소가 아닌 위도와 경도로 변경되었습니다. 위의 답변에

stack overflow link for route path between source and destination

덕분에 우리는 변화를 만들어 경로 방향을 얻을 수 있습니다.

public class DrawMapActivity extends MapActivity { 
    MapView myMapView = null; 
    MapController myMC = null; 
    GeoPoint geoPoint = null; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.main); 
     myMapView = (MapView) findViewById(R.id.mapview); 

     geoPoint = null; 
     myMapView.setSatellite(false); 
     double fromLat = 12.303534; 
     double fromLong = 76.64611; 
     double toLat = 12.9715987; 
     double toLong = 77.5945627; 

     String sourceLat = Double.toString(fromLat); 
     String sourceLong = Double.toString(fromLong); 
     String destinationLat = Double.toString(toLat); 
     String destinationLong = Double.toString(toLong); 

     String pairs[] = getDirectionData(sourceLat,sourceLong, destinationLat, destinationLong); 
     String[] lngLat = pairs[0].split(","); 

     // STARTING POINT 
     GeoPoint startGP = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6), (int) (Double.parseDouble(lngLat[0]) * 1E6)); 

     myMC = myMapView.getController(); 
     geoPoint = startGP; 
     myMC.setCenter(geoPoint); 
     myMC.setZoom(10); 
     myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP)); 

     // NAVIGATE THE PATH 

     GeoPoint gp1; 
     GeoPoint gp2 = startGP; 

     for (int i = 1; i < pairs.length; i++) { 
     lngLat = pairs[i].split(","); 
     gp1 = gp2; 
     // watch out! For GeoPoint, first:latitude, second:longitude 

     gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),(int) (Double.parseDouble(lngLat[0]) * 1E6)); 
     myMapView.getOverlays().add(new DirectionPathOverlay(gp1, gp2)); 
     Log.d("xxx", "pair:" + pairs[i]); 
     } 

     // END POINT 
     myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2)); 

     myMapView.getController().animateTo(startGP); 
     myMapView.setBuiltInZoomControls(true); 
     myMapView.displayZoomControls(true); 

    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    private String[] getDirectionData(String sourceLat, String sourceLong, String destinationLat, String destinationLong) { 


     String urlString = "http://maps.google.com/maps?f=d&hl=en&" +"saddr="+sourceLat+","+sourceLong+"&daddr="+destinationLat+","+destinationLong + "&ie=UTF8&0&om=0&output=kml"; 
     Log.d("URL", urlString); 
     Document doc = null; 
     HttpURLConnection urlConnection = null; 
     URL url = null; 
     String pathConent = ""; 

     try { 

     url = new URL(urlString.toString()); 
     urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setRequestMethod("GET"); 
     urlConnection.setDoOutput(true); 
     urlConnection.setDoInput(true); 
     urlConnection.connect(); 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     doc = db.parse(urlConnection.getInputStream()); 

     } catch (Exception e) { 
     } 

     NodeList nl = doc.getElementsByTagName("LineString"); 
     for (int s = 0; s < nl.getLength(); s++) { 
     Node rootNode = nl.item(s); 
     NodeList configItems = rootNode.getChildNodes(); 
     for (int x = 0; x < configItems.getLength(); x++) { 
     Node lineStringNode = configItems.item(x); 
     NodeList path = lineStringNode.getChildNodes(); 
     pathConent = path.item(0).getNodeValue(); 
     } 
     } 
     String[] tempContent = pathConent.split(" "); 
     return tempContent; 
    } 

    } 


    //***************************************************************************** 



    class DirectionPathOverlay extends Overlay { 

     private GeoPoint gp1; 
     private GeoPoint gp2; 

     public DirectionPathOverlay(GeoPoint gp1, GeoPoint gp2) { 
      this.gp1 = gp1; 
      this.gp2 = gp2; 
     } 

     @Override 
     public boolean draw(Canvas canvas, MapView mapView, boolean shadow, 
       long when) { 
      // TODO Auto-generated method stub 
      Projection projection = mapView.getProjection(); 
      if (shadow == false) { 

       Paint paint = new Paint(); 
       paint.setAntiAlias(true); 
       Point point = new Point(); 
       projection.toPixels(gp1, point); 
       paint.setColor(Color.BLUE); 
       Point point2 = new Point(); 
       projection.toPixels(gp2, point2); 
       paint.setStrokeWidth(2); 
       canvas.drawLine((float) point.x, (float) point.y, (float) point2.x,(float) point2.y, paint); 
      } 
      return super.draw(canvas, mapView, shadow, when); 
     } 

     @Override 
     public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
      // TODO Auto-generated method stub 

      super.draw(canvas, mapView, shadow); 
     } 

    } 

위의 코드는지도보기와는 아무 상관이없는, 그것이 의도로 UR 장치에 구글지도 응용 프로그램을 엽니 다