Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在最大缩放级别平移MapView时GeoPoint和/或android.maps.Projection的奇怪行为大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我遇到了一个奇怪的问题,点使用谷歌地图包获得抵消.

我有@L_401_0@mapView,上面显示了一条路线.我注意到,如果我一直放大,然后平移,我的路线就会被修剪到窗口边缘附近.在对我自己的代码进行了大量调试之后,我发现即使是这样一个简单的案例也足以重现这个问题:

GeoPoint midPoint = projection.fromPixels(mapView.getWidth()/2,mapView.getHeight()/2);
            GeoPoint nextPoint = new GeoPoint(midPoint.getLatitudeE6(),midPoint.getLongitudeE6());
            pathPaint.setColor(0xFF00FF00);
            projection.toPixels(midPoint,screenPoint);
            canvas.drawCircle(screenPoint.x,screenPoint.y,5,pathPaint);
            pathPaint.setColor(0xFF0000FF);
            projection.toPixels(nextPoint,pathPaint);

这使用MapView.getProjection()返回的Projection类将像素坐标转换为GeoPoint.然后使用相同的纬度/经度创建第二个GeoPoint.最后,它将这两个GeoPoints转换回屏幕坐标,并在此位置绘制两个圆圈,一个在另一个的顶部.

除了,当你平移时,第二个圆圈偏离第一个圆圈.它们具有相同的纬度/经度坐标(我在绘图后检查),但最终会转换为不同的屏幕坐标.我的调试主要集中在横向平移,但我认为垂直平移可能会遇到类似的问题,基于我在原始应用程序代码中看到的内容.

据我所知,map包创建的任何点都可以正常工作,但通过调用new GeoPoint(lat,lon)创建的任何点都会出现这种情况.

我创建了一个简单的精简应用程序,展示了这种行为,粘贴在下面. (地图图块不会下载,因为我没有费心去签名,但你不需要它们来查看问题.)只需启动它,一直放大,然后开始向左或向右平移.观察绿色和蓝色点的分歧,并见证Log.d打印,表明它们仍然具有相同的纬度/经度.

(有没有更好/首选的方式发布示例项目?对不起,这是我在这里发布的第一篇文章.)

SRC /示例/ MYSAMPLE / MapOverlay.java:

package example.mysample;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.view.MotionEvent;

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

import android.util.Log;

class MapOverlay extends Overlay
{
    public MapOverlay(MapScreen activity)
    {
        super();

        paint = new Paint();
        paint.setStyle(Paint.Style.FILL_AND_stroke);
        paint.setAntiAlias(true);
    }

    @Override public Boolean onTouchEvent(MotionEvent e,MapView mapView) {
        int motionAction = e.getAction();
        if ( motionAction == MotionEvent.ACTION_MOVE ) {
            dragging = true;
        }
        if ( motionAction == MotionEvent.ACTION_UP || motionAction == MotionEvent.ACTION_CANCEL ) {
            dragging = false;
        }
        return super.onTouchEvent(e,mapView);
    }

    @Override public void draw(Canvas canvas,MapView mapView,Boolean shadow) {
        if (!shadow && !dragging) {
            Projection projection = mapView.getProjection();

            // Draw two circles at the center
            // One with a virgin point,one that we constructed with the same coordinates
            GeoPoint midPoint = projection.fromPixels(mapView.getWidth()/2,midPoint.getLongitudeE6());
            paint.setColor(0xFF00FF00);
            projection.toPixels(midPoint,paint);
            paint.setColor(0xFF0000FF);
            projection.toPixels(nextPoint,paint);

            // these print EQUAl.
            Log.d("BKC DEBUG","Midpoint and our own midpoint have " +
                ((midPoint.getLatitudeE6() == nextPoint.getLatitudeE6()) ? "EQUAL" : "UNEQUAL") +
                " latitudes,and " +
                ((midPoint.getLongitudeE6() == nextPoint.getLongitudeE6()) ? "EQUAL" : "UNEQUAL") +
                " longitudes");
        }
    }

    private Paint paint;
    private Boolean dragging;
    private Point screenPoint = new Point();

}

SRC /示例/ MYSAMPLE / MapScreen.java:

package example.mysample;

import android.os.bundle;
import android.view.View;
import android.widget.Toast;
import android.util.Log;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.GeoPoint;

public class MapScreen extends MapActivity
{

    @Override public void onCreate(Bundle iciclE) {
        super.onCreate(iciclE);
        setContentView(R.layout.map);

        mapView = (MapView)findViewById(R.id.map_map);
        mapView.setBuilTinZoomControls(true);
        mapView.getOverlays().add(new MapOverlay(this));

        MapController controller;
        controller = mapView.getController();
        controller.setZoom(17);
        controller.animateTo(new GeoPoint(36000000,-90000000));

    }

    @Override
    public Boolean isRouteDisplayed() {
        return true;
    }

    private MapView mapView;

}

RES /布局/ map.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://scheR_14_11845@as.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:BACkground="#414141"
    >    

 <com.google.android.maps.MapView
            android:id="@+id/map_map"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="3dip"
            android:layout_marginRight="3dip"
            android:layout_marginTop="3dip"
            android:layout_marginBottom="3dip"
            android:apiKey="0jPQdwUtQGBYfPALki6ghGG_X9Jpf-SllYckR4w"
            android:layout_centerInParent="true"
            android:clickable="true"
            />

</RelativeLayout>

AndroidManifest.xml中:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://scheR_14_11845@as.android.com/apk/res/android"
      package="example.mysample"
      android:versionCode="1"
      android:versionName="2.0.1">
    <uses-sdk android:minSdkVersion="4" />

    <!--uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /-->
    <!--uses-permission android:name="android.permission.READ_PHONE_STATE" /-->
    <uses-permission android:name="android.permission.INTERNET" />
    <!--uses-permission android:name="android.permission.WAKE_LOCK" /-->
    <!--uses-permission android:name="android.permission.READ_LOGS" /-->
    <!--uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /-->

    <!--application android:name="src.example.mysample.AppMain"
                 android:label="MySample"
                 android:icon="@drawable/icon_android"
                 android:theme="@android:style/Theme.NotitleBar"
                 -->

    <application android:name="android.app.Application"
        android:label="MySample" >

        <uses-library android:name="com.google.android.maps"/>

        <!-- Main activity -->
        <activity android:name="example.mysample.MapScreen"
                  android:clearTaskOnLaunch="true"
                  android:label="MySample"
                  android:screenOrientation="porTrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Other activities -->
        <!--activity android:name="example.mysample@L_664_17@methingElse"
                  android:label="Something"
                  android:screenOrientation="porTrait"/-->

    </application>
</manifest>

build.xml(从模板不变):

<?xml version="1.0" encoding="UTF-8"?>
<project name="PROjeCt_name" default="Help">

<!-- The local.properties file is created and updated by the 'android'
     tool.
     It contains the path to the SDK. It should *NOT* be checked into
     Version Control Systems. -->
    <property file="local.properties" />

    <!-- The build.properties file can be created by you and is never touched
         by the 'android' tool. This is the place to change some of the
         default property values used by the Ant rules.
         Here are some properties you may want to change/update:

         source.dir
             The name of the source directory. Default is 'src'.
         out.dir
             The name of the output directory. Default is 'bin'.

         Properties related to the SDK LOCATIOn or the project target should
         be updated using the 'android' tool with the 'update' action.

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems.

         -->
    <property file="build.properties" />

    <!-- The default.properties file is created and updated by the 'android'
         tool,as well as ADT.
         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems. -->
    <property file="default.properties" />

    <!-- Custom Android task to deal with the project target,and import the
         proper rules.
         This requires ant 1.6.0 or above. -->
    <path id="android.antlibs">
        <patHelement path="${sdk.dir}/tools/lib/anttasks.jar" />
        <patHelement path="${sdk.dir}/tools/lib/sdklib.jar" />
        <patHelement path="${sdk.dir}/tools/lib/androidprefs.jar" />
    </path>

    <taskdef name="setup"
        classname="com.android.ant.SetupTask"
        classpathref="android.antlibs" />

<!-- extension targets. Uncomment the ones where you want to do custom work
     in between standard targets -->
<!--
    <target name="-pre-build">
    </target>
    <target name="-pre-compile">
    </target>

    [This is typically used for code obfuscation.
     Compiled code LOCATIOn: ${out.classes.absolute.dir}
     If this is not done in place,override ${out.dex.input.absolute.dir}]
    <target name="-post-compile">
    </target>
-->


    <!-- Execute the Android Setup task that will setup some properties
         specific to the target,and import the build rules files.

         The rules file is imported from
            <SDK>/platforms/<target_platform>/ant/ant_rules_r#.xml

         To customize exisTing targets,there are two options:
         - Customize only one target:
             - copy/paste the target into this file,*before* the
               <setup> task.
             - customize it to your needs.
         - Customize the whole script.
             - copy/paste the content of the rules files (minus the top nodE)
               into this file,*after* the <setup> task
             - disable the import of the rules by changing the setup task
               below to <setup import="false" />.
             - customize to your needs.
    -->
    <setup />

</project>

解决方法

我也面临这个问题 – 似乎是Android Mapview API中的一个错误 – 我认为Star的问题就是这个:
http://code.google.com/p/android/issues/detail?id=17387&can=5&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars 还有一个可能的解决方法是丑陋的,但适用于评论 – 基本上你确定错误并相应地调整结果

大佬总结

以上是大佬教程为你收集整理的在最大缩放级别平移MapView时GeoPoint和/或android.maps.Projection的奇怪行为全部内容,希望文章能够帮你解决在最大缩放级别平移MapView时GeoPoint和/或android.maps.Projection的奇怪行为所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。