2011-12-18 163 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); 

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

相关问题