当地点发生变化时(或者当我通过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正确显示了lontitute和latitute,但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;
}
}
答案 0 :(得分: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);
答案 1 :(得分:0)
您需要在mapview.invalidate();
之后添加mapController.animateTo(geopoint)
GeoPoint geopoint = new GeoPoint(latitute, lontitue);
mapController.animateTo(geopoint);
mapview.invalidate();
这应该有用!