Android:地图控制器的 animateTo 方法无法正常工作

发布于 2024-11-17 05:37:14 字数 3071 浏览 4 评论 0原文

当位置更改时(或者当我通过 telnet 提供模拟位置时),我试图在地图上为新位置设置动画,

这就是我使用的

Toast.makeText(getApplicationContext(), "New Lontitue = "+ lontitue +"\n New Latitute = "+ latitute, Toast.LENGTH_LONG).show();

GeoPoint geopoint = new GeoPoint(latitute, lontitue);
mapController.animateTo(geopoint);

虽然 Toast 正确显示了经度和纬度,但应用程序不会为给定的位置设置动画坐标。可能是什么问题?请在下面找到我的完整代码。

package com.mayuonline.androidgps;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
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.MapController;
import com.google.android.maps.MapView;

public class GPSActivity extends MapActivity  {
    MapController mapController; 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // setting up map
        MapView mapview = (MapView)findViewById(R.id.mapview);
        mapview.setBuiltInZoomControls(true);

        mapController = mapview.getController();
       // mapController.setZoom(16);

        LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);

        LocationListener locationListener = new LocationListener() {

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onLocationChanged(Location location) {
                makeUseOfNewLocation(location);
                //Toast.makeText(getApplicationContext(), "New Locationdd", Toast.LENGTH_LONG).show();              
            }

            private void makeUseOfNewLocation(Location location) {

                double lon = (double)location.getLongitude();
                double lat = (double)location.getLatitude();

                int lontitue = (int)lon;
                int latitute = (int)lat;

                Toast.makeText(getApplicationContext(), "New Lontitue = "+ lontitue +"\n New Latitute = "+ latitute, Toast.LENGTH_LONG).show();

                GeoPoint geopoint = new GeoPoint(latitute, lontitue);
                mapController.animateTo(geopoint);
            }
        };

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

I'm trying to animate a new location on the map when the location changed (or when I supply a mock location via the telnet)

this is what I use

Toast.makeText(getApplicationContext(), "New Lontitue = "+ lontitue +"\n New Latitute = "+ latitute, Toast.LENGTH_LONG).show();

GeoPoint geopoint = new GeoPoint(latitute, lontitue);
mapController.animateTo(geopoint);

Though the Toast shows the lontitute and latitute properly, app doesn't animate to given cordinates. What could be the problem?? Please find my whole code below.

package com.mayuonline.androidgps;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
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.MapController;
import com.google.android.maps.MapView;

public class GPSActivity extends MapActivity  {
    MapController mapController; 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // setting up map
        MapView mapview = (MapView)findViewById(R.id.mapview);
        mapview.setBuiltInZoomControls(true);

        mapController = mapview.getController();
       // mapController.setZoom(16);

        LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);

        LocationListener locationListener = new LocationListener() {

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onLocationChanged(Location location) {
                makeUseOfNewLocation(location);
                //Toast.makeText(getApplicationContext(), "New Locationdd", Toast.LENGTH_LONG).show();              
            }

            private void makeUseOfNewLocation(Location location) {

                double lon = (double)location.getLongitude();
                double lat = (double)location.getLatitude();

                int lontitue = (int)lon;
                int latitute = (int)lat;

                Toast.makeText(getApplicationContext(), "New Lontitue = "+ lontitue +"\n New Latitute = "+ latitute, Toast.LENGTH_LONG).show();

                GeoPoint geopoint = new GeoPoint(latitute, lontitue);
                mapController.animateTo(geopoint);
            }
        };

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神经大条 2024-11-24 05:37:14

我想通了这个问题。这与 getLongitude 方法有关:)

我需要乘以 1E6,如下所示。

double lon = (double) (location.getLongitude() * 1E6);
double lat = (double) (location.getLatitude() * 1E6);

int lontitue = (int)lon;
int latitute = (int)lat;

Toast.makeText(getApplicationContext(), "New Lontitue = "+ lontitue +"\n New Latitute = "+ latitute, Toast.LENGTH_LONG).show();

GeoPoint geopoint = new GeoPoint(latitute, lontitue);
mapController.animateTo(geopoint);

I figured out the issue. It is something to do with getLongitude method :)

I need to multiply by 1E6 as given below.

double lon = (double) (location.getLongitude() * 1E6);
double lat = (double) (location.getLatitude() * 1E6);

int lontitue = (int)lon;
int latitute = (int)lat;

Toast.makeText(getApplicationContext(), "New Lontitue = "+ lontitue +"\n New Latitute = "+ latitute, Toast.LENGTH_LONG).show();

GeoPoint geopoint = new GeoPoint(latitute, lontitue);
mapController.animateTo(geopoint);
你不是我要的菜∠ 2024-11-24 05:37:14

您需要像这样在 mapController.animateTo(geopoint) 之后添加 mapview.invalidate();

    GeoPoint geopoint = new GeoPoint(latitute, lontitue);
        mapController.animateTo(geopoint);
        mapview.invalidate();

应该可以工作!

You need to add mapview.invalidate(); after your mapController.animateTo(geopoint) like this

    GeoPoint geopoint = new GeoPoint(latitute, lontitue);
        mapController.animateTo(geopoint);
        mapview.invalidate();

That should work!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文