2011-12-18 162 views
0

我試圖製作一個獲取用戶位置的應用程序,並將用戶引導到地圖上的某個位置。我有兩個類:將座標從一個類傳遞到另一個類

package com.pizzeria.uno; 

import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.widget.Toast; 

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

public class ComoLlegar extends MapActivity { 

private MapView mapView; 
private MyLocationOverlay myLocationOverlay; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // main.xml contains a MapView 
    setContentView(R.layout.llegar); 

    // extract MapView from layout 
    mapView = (MapView) findViewById(R.id.mapview); 
    mapView.setBuiltInZoomControls(true); 

    // create an overlay that shows our current location 
    myLocationOverlay = new FixedMyLocationOverlay(this, mapView); 

    // add this overlay to the MapView and refresh it 
    mapView.getOverlays().add(myLocationOverlay); 
    mapView.postInvalidate(); 
    mapView.setSatellite(true); 



    GeoPoint point1 = new GeoPoint(43467604,-3833841); 

    double lat1 = (point1.getLatitudeE6()/ 1E6); 
    double lng1 = (point1.getLongitudeE6()/ 1E6); 

    GeoPoint point2 = new GeoPoint(39639538,-4921875); 

    double lat2 = (point2.getLatitudeE6()/ 1E6); 
    double lng2 = (point2.getLongitudeE6()/ 1E6); 




    final Intent intent = new Intent(Intent.ACTION_VIEW, 

      Uri.parse(
        "http://maps.google.com/maps?" + 
        "saddr=" + lat2 + "," + lng2 + 
        "&daddr=" + lat1 + "," + lng1)); 
      startActivity(intent); 

    myLocationOverlay.disableMyLocation(); 

    // call convenience method that zooms map on our location 
    zoomToMyLocation(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    // when our activity resumes, we want to register for location updates 
    myLocationOverlay.enableMyLocation(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    // when our activity pauses, we want to remove listening for location updates 
    myLocationOverlay.disableMyLocation(); 
} 

/** 
* This method zooms to the user's location with a zoom level of 10. 
*/ 
private void zoomToMyLocation() { 

    double lat=43.467604; 
    double lng=-3.833841; 
    GeoPoint myLocationGeoPoint = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6)); 
    if(myLocationGeoPoint != null) { 
     mapView.getController().animateTo(myLocationGeoPoint); 
     mapView.getController().setZoom(13); 
    } 
    else { 
     Toast.makeText(this, "Cannot determine location", Toast.LENGTH_SHORT).show(); 
    } 
} 






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

我需要的是改變這種 「GeoPoint的點2 =新的GeoPoint(39639538,-4921875);」爲此,我從這個類得到的座標:

package com.pizzeria.uno; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Point; 
import android.graphics.Paint.Style; 
import android.graphics.drawable.Drawable; 
import android.location.Location; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapView; 
import com.google.android.maps.MyLocationOverlay; 
import com.google.android.maps.Projection; 


public class FixedMyLocationOverlay extends MyLocationOverlay { 

private boolean bugged = false; 

private Drawable drawable; 
private Paint accuracyPaint; 
private Point center; 
private Point left; 
private int width; 
private int height; 

public FixedMyLocationOverlay(Context context, MapView mapView) { 
    super(context, mapView); 
} 

@Override 
protected void drawMyLocation(Canvas canvas, MapView mapView, 
     Location lastFix, GeoPoint myLocation, long when) { 
    if(!bugged) { 
     try { 
      super.drawMyLocation(canvas, mapView, lastFix, myLocation, when); 
     } catch (Exception e) { 
      // we found a buggy phone, draw the location icons ourselves 
      bugged = true; 
     } 
    } 

    if(bugged) { 
     if(drawable == null) { 

      accuracyPaint = new Paint(); 
      accuracyPaint.setAntiAlias(true); 
      accuracyPaint.setStrokeWidth(2.0f); 

      drawable = mapView.getContext().getResources().getDrawable(R.drawable.icon); 
      width = drawable.getIntrinsicWidth(); 
      height = drawable.getIntrinsicHeight(); 
      center = new Point(); 
      left = new Point(); 
     } 

     Projection projection = mapView.getProjection(); 
     double latitude = lastFix.getLatitude(); 
     double longitude = lastFix.getLongitude(); 
     float accuracy = lastFix.getAccuracy(); 

     float[] result = new float[1]; 

     Location.distanceBetween(latitude, longitude, latitude, longitude + 1, result); 
     float longitudeLineDistance = result[0]; 

     GeoPoint leftGeo = new GeoPoint((int)(latitude*1e6), (int)((longitude-accuracy/longitudeLineDistance)*1e6)); 
     projection.toPixels(leftGeo, left); 
     projection.toPixels(myLocation, center); 
     int radius = center.x - left.x; 

     accuracyPaint.setColor(0xff6666ff); 
     accuracyPaint.setStyle(Style.STROKE); 
     canvas.drawCircle(center.x, center.y, radius, accuracyPaint); 

     accuracyPaint.setColor(0x186666ff); 
     accuracyPaint.setStyle(Style.FILL); 
     canvas.drawCircle(center.x, center.y, radius, accuracyPaint); 

     drawable.setBounds(center.x - width/2, center.y - height/2, center.x + width/2, center.y + height/2); 
     drawable.draw(canvas); 
    } 
} 
} 

的問題是,我不能夠得到從第二類的座標頭等。

回答

0

您可以嘗試

Intent i = new Intent(); 
i.putExtra("points", geoPoints); 

在第二級呼叫

Intent i = getIntent(); 
i.getExtra("points"); 
+0

謝謝你的回覆。我已經嘗試了兩種方法,但我無法解決我的問題。總是我得到「空指針錯誤」。 – Aldridge1991 2011-12-19 00:02:12

0

如果你的第二個活動僅用於計算一個座標,然後返回該數據,那麼你應該調用該活動通過startActivityForResult,參見this。它讓你「提示」一個結果的活動。

+0

任何幫助都會非常感謝 – Aldridge1991 2011-12-19 14:05:46

1

對於這個問題,爲ComoLlegar.class創建SINGLETON類,並引用FixedMyLocationOverlay.class。現在您可以訪問所有對象,並可以將值發送到First類。

參考: 寫代碼ComoLlegar.class:

public static MapActivity getMapObject() { 

    if (mapObject != null) { 
     return mapObject; 
    } 
    return mapObject; 

} 

寫代碼Second.class

((ComoLlegar.class) object).method_name(arguments); 

現在,它的工作原理:) :)

相關問題