Aldridge1991
Aldridge1991

Reputation: 1367

Passing coordinates from one class to another

I'm trying to make an app which gets user location, and guides the user to a certain place on the map. I've got two classes:

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

What I need is to change this "GeoPoint point2 = new GeoPoint(39639538,-4921875);" for the coordinates which I get from this class:

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

The problem is that I'm not able to get the coordinates from the second class to the first class.

Upvotes: 0

Views: 731

Answers (3)

Nagendra Badiganti
Nagendra Badiganti

Reputation: 78

For this problem, Create SINGLETON class for the ComoLlegar.class and make a reference to FixedMyLocationOverlay.class. Now you can access all the objects and you can send values to the First class.

Reference: Write this code in ComoLlegar.class:

public static MapActivity getMapObject() {

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

}

Write this code in Second.class

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

Now it works :):)

Upvotes: 1

soren.qvist
soren.qvist

Reputation: 7416

If your second activity only serves to calculate a coordinate and then return that data, then you should call that activity through startActivityForResult, see this. It let's you "prompt" an activity for a result.

Upvotes: 0

coder_For_Life22
coder_For_Life22

Reputation: 26971

You can try

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

in your second class call

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

Upvotes: 0

Related Questions