我正在尝试创建一个获取用户位置的应用,并引导用户到地图上的某个位置。我有两节课:
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 point2 = new 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 :(得分: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);
现在可行:):))
答案 1 :(得分:0)
你可以尝试
Intent i = new Intent();
i.putExtra("points", geoPoints);
在你的第二堂课中
Intent i = getIntent();
i.getExtra("points");
答案 2 :(得分:0)
如果您的第二项活动仅用于计算坐标,然后返回该数据,则应通过startActivityForResult
调用该活动,请参阅this。它让你"提示"结果的活动。