2012-08-09 201 views
0

以下是我的課程,它在Google地圖上的當前位置繪製圖像。我需要在谷歌地圖上再繪製兩個位置。假設A點有以下值 -在Google地圖上顯示當前位置附近的多個位置

lat1: 37.33774833333334 
long1: -121.88670166666667 

B點有以下值 -

lat2: 37.336453 
long2: -121.884985 

我要畫點A和點B中的谷歌地圖的圖像上。我的繪圖文件夾中已經有了這兩個點的圖像。

public class MainActivity extends MapActivity {  
    public static final String TAG = "GoogleMapsActivity"; 
    private MapView mapView; 
    private LocationManager locationManager; 
    Geocoder geocoder; 
    Location location; 
    LocationListener locationListener; 
    CountDownTimer locationtimer; 
    MapController mapController; 
    MapOverlay mapOverlay = new MapOverlay(); 

// Point A 
    private Double lat1 = 37.33774833333334; 
    private Double long1 = -121.88670166666667 ; 

// Point B    
    private Double lat2 = 37.336453; 
    private Double long2 = -121.884985; 

    /** Called when the activity is first created. */ 
    @Override 
    protected void onCreate(Bundle icicle) { 
     try {  
      super.onCreate(icicle); 
      setContentView(R.layout.activity_main); 
      initComponents(); 
      mapView.setBuiltInZoomControls(true); 
      mapView.setSatellite(false); 
      mapController = mapView.getController(); 
      mapController.setZoom(15); 
      locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
      if (locationManager == null) { 
       Toast.makeText(getApplicationContext(), 
         "Location Manager Not Available", Toast.LENGTH_SHORT) 
         .show(); 
       return; 
      } 
      location = locationManager 
      .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      if (location == null) 
       location = locationManager 
       .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
      if (location != null) { 
       double lat = location.getLatitude(); 
       double lng = location.getLongitude(); 
       Toast.makeText(getApplicationContext(), 
         "Location Are" + lat + ":" + lng, Toast.LENGTH_SHORT) 
         .show(); 
       GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)); 
       mapController.animateTo(point, new Message()); 
       mapOverlay.setPointToDraw(point); 
       List<Overlay> listOfOverlays = mapView.getOverlays(); 
       listOfOverlays.clear(); 
       listOfOverlays.add(mapOverlay); 
      } 
      locationListener = new LocationListener() { 
       @Override 
       public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
       } 

       @Override 
       public void onProviderEnabled(String arg0) { 
       } 

       @Override 
       public void onProviderDisabled(String arg0) { 
       } 

       @Override 
       public void onLocationChanged(Location l) { 
        location = l; 
        locationManager.removeUpdates(this); 
        if (l.getLatitude() == 0 || l.getLongitude() == 0) { 
        } else { 
         double lat = l.getLatitude(); 
         double lng = l.getLongitude(); 
         Toast.makeText(getApplicationContext(), 
           "Location Are" + lat + ":" + lng, 
           Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }; 
      if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
       locationManager.requestLocationUpdates(
         LocationManager.GPS_PROVIDER, 1000, 10f, locationListener); 
      if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ 
       locationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener); 
      } 
      locationtimer = new CountDownTimer(30000, 5000) { 
       @Override 
       public void onTick(long millisUntilFinished) { 
        if (location != null) 
         locationtimer.cancel(); 
       } 

       @Override 
       public void onFinish() { 
        if (location == null) { 
        } 
       } 
      }; 
      locationtimer.start();   

     } 
     catch(Exception e) { 
      System.out.println(e); 
     } 
    } 

    public MapView getMapView() { 
     return this.mapView; 
    } 

    private void initComponents() { 
     mapView = (MapView) findViewById(R.id.mapView); 
    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     return false; 
    } 

The following class is the continuation from the above。在下面的課程中,我將在Google地圖上顯示我的當前位置。所以我認爲我需要在這個課堂上進行修改,才能在Google地圖上顯示這兩點。

class MapOverlay extends Overlay { 
     private GeoPoint pointToDraw; 

     public void setPointToDraw(GeoPoint point) { 
      pointToDraw = point; 
     } 

     public GeoPoint getPointToDraw() { 
      return pointToDraw; 
     } 

     @Override 
     public boolean draw(Canvas canvas, MapView mapView, boolean shadow, 
       long when) { 
      super.draw(canvas, mapView, shadow); 

      Point screenPts = new Point(); 
      mapView.getProjection().toPixels(pointToDraw, screenPts); 

      Bitmap bmp = BitmapFactory.decodeResource(getResources(), 
        R.drawable.current_user); 
      canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); 
      return true; 
     } 
    } 

} 

任何想法將不勝感激,我怎麼能在Google地圖上顯示兩個點A和點B.

+0

本主題中的答案可能會有所幫助。 http://stackoverflow.com/questions/10573586/adding-itemizedoverlay-to-my-mapview – 2012-08-09 08:37:46

回答

1

使用下面的代碼來繪製點A和點B,但添加在此之前的代碼檢查出此鏈接並創建HelloItemizedOverlay類:https://developers.google.com/maps/documentation/android/hello-mapview

HelloItemizedOverlay iOverlay1 = new ItemizedOverlay(R.drawable.pointAimage, MainActivity.this); 
HelloItemizedOverlay iOverlay2 = new ItemizedOverlay(R.drawable.pointBimage, MainActivity.this); 
GeoPoint pointA = new GeoPoint(37337748, -12188670); 
GeoPoint pointB = new GeoPoint(37336453, -12188498); 
OverlayItem overlayItemA = new OverlayItem(pointA, "Title", "Body"); 
OverlayItem overlayItemB = new OverlayItem(pointB, "Title", "Body"); 
iOverlay1.addOverlay(overlayItemA); 
iOverlay2.addOverlay(overlayItemB); 
相關問題