2

I am getting different x,y value for a specific lat,long. I wonder why it is happening. May be I lack knowledge. So, if you anyone can explain by observing the following code snippet then it would be a great help.

import java.util.List;

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


import android.app.Activity;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

public class MaopaoActivity extends MapActivity {


        MapView FMmapview;
        GeoPoint fm_gp;
        Button bt;
        int lat,lng;

        List<Overlay> mapOverlays;
        Drawable drawable;
        CustomItemizedOverlay itemizedoverlay;
        OverlayItem overlayitem;
        String RASTA="";
        String [] pairs;
        int EndLat,EndLng;
        String kotha;
        TextView tv;
        double startlat,startlong;
        double endlat,endlong;
        double midlat,midlong;


        int startlatE6,startlongE6;
        int endlatE6,endlongE6;
        int midlatE6,midlongE6;
        GeoPoint StartGp,EndGp,midGp1,midGp2;
        Integer x1,y1,x2,y2,x3,y3;
        Double x4,y4;
        Point StartPoint;
        Point EndPoint;
        Point MidPoint1;
        Point MidPoint2;
        String abc;

        double w,T,G;
        Integer Tint,Gint;




        @Override
        protected void onCreate(Bundle icicle)
        {
            // TODO Auto-generated method stub
            super.onCreate(icicle);
            setContentView(R.layout.fullmap);

            StartPoint = new Point();
            EndPoint = new Point();
            MidPoint1 = new Point();
            MidPoint2 = new Point();
            tv=(TextView)findViewById(R.id.fm_tv1);         

            startlat=34.123018;
            startlatE6=(int)(startlat*1E6);
            startlong=-118.146278;
            startlongE6=(int)(startlong*1E6);

            endlat=34.122525;
            endlatE6=(int)(endlat*1000000);
            endlong=-118.146241;
            endlongE6=(int)(endlong*1000000);

            midlat=34.122724;
            midlatE6=(int)(midlat*1000000);
            midlong=-118.146106;
            midlongE6=(int)(midlong*1000000);


            EndGp=new GeoPoint(endlatE6,endlongE6);
            midGp1=new GeoPoint(midlatE6,midlongE6);
            StartGp=new GeoPoint(startlatE6,startlongE6);


            FMmapview=(MapView)findViewById(R.id.MapViewFM123);

            Projection projection = FMmapview.getProjection();


            FMmapview.getController().setCenter(StartGp);
            FMmapview.getController().setZoom(18);


            FMmapview.getProjection().toPixels(EndGp,EndPoint);
            FMmapview.getProjection().toPixels(midGp1,MidPoint1);
            FMmapview.getProjection().toPixels(StartGp,StartPoint);

            x1=StartPoint.x;
            y1=StartPoint.y;

            x2=EndPoint.x;
            y2=EndPoint.y;

            x3=MidPoint1.x;
            y3=MidPoint1.y;


            w=(y2-y1)/(x2-x1);



            T=((x3+(w*w*x1)+(w*y3)-(w*x1))/(w*w+1));
            G=w*(T-x1)+y1;

            Tint=(int) Math.ceil(T);
            Gint=(int)Math.ceil(G);



            mapOverlays = FMmapview.getOverlays();
            drawable = this.getResources().getDrawable(R.drawable.blue);
            itemizedoverlay = new CustomItemizedOverlay(drawable,this);
            overlayitem = new OverlayItem(midGp1, "Your are at","hello motttto");
            itemizedoverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedoverlay);



            midGp2=FMmapview.getProjection().fromPixels(Tint,Gint);

            //x4=(midGp2.getLatitudeE6())/(1E6);
            //y4=(midGp2.getLongitudeE6())/(1E6);


            abc="A="+x1.toString()+','+y1.toString()+" B="+x2.toString()+","+y2.toString()+" C="+x3.toString()+","+y3.toString()+" D="+Tint.toString()+","+Gint.toString();
            tv.setText(abc);

            //midGp2=new GeoPoint(34048039,-118140655);

            mapOverlays = FMmapview.getOverlays();
            drawable = this.getResources().getDrawable(R.drawable.blue);
            itemizedoverlay = new CustomItemizedOverlay(drawable,this);
            overlayitem = new OverlayItem(midGp2, "Your are at","hello motttto");
            itemizedoverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedoverlay);



            FMmapview.getController().animateTo(StartGp);
            FMmapview.getController().setCenter(StartGp);
            FMmapview.getController().setZoom(18);
            FMmapview.getOverlays().add(new MyOverLay(StartGp,EndGp,2,Color.BLUE));     


        }

        @Override
        protected boolean isRouteDisplayed()
        {
            // TODO Auto-generated method stub
            return false;
        }

    }

Here I get x,y=0,0 for the geopoint 34.123018,-118.146278; and again I get x,y=240,340 when I re-run the app.

If I clear data, I get x,y=0,0 and then When I re-run I get different value for the same aforementioned lat/long.

It would be a great help if anyone can clear my conception

EDITED:

I have found the reason, which is totally unusual to me. The projection value to pixel may vary.

as http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MapView.html#getProjection%28%29 says that "The Projection of the map in its current state. You should not hold on to this object for more than one draw, since the projection of the map could change."

So, x,y can be 0,0 or can be anything. Whatever might be the projected x,y value is, it will always represent same lat,long.

The problem that leads me to such thinking unfortunately was the following line:

T=((x3+(w*w*x1)+(w*y3)-(w*x1))/(w*w+1));

It provides different value. If I break this into 3 lines then T provides correct result. Though, I never had to face such situation in C++. This is something weird.

Anyway, I would still want to know the reason behind the value of the projection being different.

1 Answer 1

0

I believe the answer to your question is that the zoom function in MapView is animated, that means it doesn’t complete immediately. So, the different projections you are getting results from them being taken during the zoom animation where the locations in the map are changing at the same time you are taking them.

To confirm that, you can make a delay of a couple of seconds between the map center/zoom change and taking the projections and you should always get the same projection values.

Good luck, Luis

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.