2011-12-18 7 views
0

私は、ユーザーの場所を取得するアプリケーションを作ろうとしており、ユーザーを地図上の特定の場所に誘導しています。私は、2つのクラス持っている:私は必要なものあるクラスから別のクラスへ座標を渡す

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); 
    } 
} 
} 

問題は、私が最初のクラスに2つ目のクラスから座標を取得することはできませんよということです。

答えて

0

2番目の活動が唯一、あなたはを通じてその活動を呼び出す必要があり、座標と、そのデータを返す計算するのに役立つ場合はあなたの2番目のクラスの呼び出し

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

をいただき、ありがとうございます返信私は両方の方法を試しましたが、問題を解決できませんでした。常に私は "ヌルポインタエラー"を取得します。 – Aldridge1991

0

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

を試すことができますstartActivityForResultthisを参照してください。それは、あなたが結果のための活動を「促す」ことを可能にします。

+0

どんな助力も非常に感謝しています – Aldridge1991

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); 

をこのコードを書く今、それは動作します:) :)

関連する問題