2011-06-24 62 views
0

我試圖動畫時的位置改變了地圖上的新位置(或者當我通過遠程登錄提供一個模擬位置)安卓:地圖的Controler的animateTo方法不能正常工作

這就是我使用

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

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

雖然Toast顯示了正確的結果,但app不會給予給定的座標動畫。可能是什麼問題呢??請在下面找到我的整個代碼。

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

回答

1

我想出了這個問題。這與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); 
+0

啊啊,這也會導致問題。對不起,我不能告訴你從你的代碼,因爲你只有變量姓名,我假設你已經有他們在正確的形式。對不起,我的回答沒有任何幫助! – 2011-06-26 02:55:17

+0

沒問題。非常感謝您的時間:)其非常感謝:) –

0

你需要你的mapController.animateTo(geopoint)後加入mapview.invalidate();這樣

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

這應該工作!

+0

沒有改變相同的結果。它指向相同的地方:((吐司工作正常,但地圖不移動 –

+0

invalidate什麼都不做:( –