2012-11-05 44 views
0
package com.chleon.mapviewdemo; 

import java.util.List; 

import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.graphics.Point; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.OverlayItem; 
import com.google.android.maps.Projection; 

public class MainActivity extends MapActivity { 
    Projection projection; 

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

     MapView mapView = (MapView) findViewById(R.id.mapview); 

     mapView.setBuiltInZoomControls(true); 

     List<Overlay> mapOverlays = mapView.getOverlays(); 
     Drawable drawable = this.getResources().getDrawable(
       R.drawable.ic_action_search); 
     HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
       drawable, this); 
     int latitude1 = (int) (28.619087 * 1e6); 
     int longitude1 = (int) (77.376867 * 1e6); 

     GeoPoint point = new GeoPoint(latitude1, longitude1); 
     OverlayItem overlayitem = new OverlayItem(point, "Prashant", 
       "I'm somewhere near delhi"); 

     GeoPoint point2 = new GeoPoint(17385812, 78480667); 
     OverlayItem overlayitem2 = new OverlayItem(point2, "Prashant", 
       "I'm in Hyderabad"); 

     itemizedoverlay.addOverlay(overlayitem); 
     itemizedoverlay.addOverlay(overlayitem2); 

     mapOverlays.add(itemizedoverlay); 
    } 

    protected boolean isRouteDisplayed() { 
     return false; 
    } 

    class MyOverlay extends Overlay { 

     public MyOverlay() { 

     } 

     public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
      super.draw(canvas, mapView, shadow); 

      Paint mPaint = new Paint(); 
      mPaint.setDither(true); 
      mPaint.setColor(Color.RED); 
      mPaint.setStyle(Paint.Style.FILL_AND_STROKE); 
      mPaint.setStrokeJoin(Paint.Join.ROUND); 
      mPaint.setStrokeCap(Paint.Cap.ROUND); 
      mPaint.setStrokeWidth(6); 
      int latitude1 = (int) (28.619087 * 1e6); 
      int longitude1 = (int) (77.376867 * 1e6); 
      projection = mapView.getProjection(); 
      GeoPoint point = new GeoPoint(latitude1, longitude1); 
      GeoPoint point2 = new GeoPoint(17385812, 78480667); 
      Point p1 = new Point(); 
      Point p2 = new Point(); 
      Path path = new Path(); 

      projection.toPixels(point, p1); 
      projection.toPixels(point2, p2); 

      path.moveTo(p2.x, p2.y); 
      path.lineTo(p1.x, p1.y); 

      canvas.drawPath(path, mPaint); 
     } 
    } 
} 

我只想繪製一條線連接代碼中給出的兩個地理座標。兩點顯示,但我無法得到我想繪製的線。 我提到Drawing a line/path on Google Maps但輸出不是我想要的。我無法在兩個地理座標之間繪製路徑。這裏是代碼

回答

相關問題