これは、前の質問の拡張の一部です: Android プログラミング、マップビューのロケーション オーバーレイ: デフォルトの青いロケーション ドットを別の画像で変更する方法
デバイスの位置を地図上に表示するように取り組んできましたが (enablemylocation で簡単に)、デフォルトのアイコンを置き換えて (簡単に並べ替えて)、GPS から取得した現在のデバイスの方位に対応するようにアイコンを回転させる必要があります。電話が向いている方向ではなく、動きの方向である必要があるためです。
「getlatitude()」と「getaltitude()」を使用して成功したのと同じ方法で「getbearing()」を使用していますが、getbearing は常に 0 を返します。問題だったアイコンは、他のことが起こる前に、ベアリングの変数がテキストビューに出力されます)
非常に奇妙なことは、5 秒間 2 回動作し、その後 0 を返すようになったことです。理論的には、コードは正しいように見えますか?
コードは次のとおりです。
敬具
ニック
主な活動:
package com.nick.kestrel;
import android.content.Context;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class KestrelActivity extends MapActivity {
/** Called when the activity is first created. */
static TextView LocationText;
MapView mapView;
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Identifies the textview 'locationtext' as the variable LocationText. Planned usage just for debugging
LocationText = (TextView)findViewById(R.id.locationtext);
//defines the mapview as variable 'mapView' and enables zoom controls
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
/**
* Code required to receive gps location. Activates GPS provider, and is set to update only after
* at least 10 seconds and a position change of at least 10 metres
*/
LocationListener locationListener = new MyLocationListener();
//setting up the location manager
LocationManager locman = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locman.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, (float) 0.01, locationListener);
//Adds a current location overlay to the map 'mapView' and turns on the map's compass
MyLocation myLocationOverlay = new MyLocation(this, mapView);
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
mapView.getOverlays().add(myLocationOverlay);
mapView.postInvalidate();
}
}
マイロケーションリスナー
package com.nick.kestrel;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
public class MyLocationListener implements LocationListener {
private boolean hasbearing;
/**
* Code to run when the listener receives a new location
*/
@Override
public void onLocationChanged(Location locFromGps) {
//Toast.makeText(getApplicationContext(), "Location changed, Lat: "+locFromGps.getLatitude()+" Long: "+ locFromGps.getLongitude(), Toast.LENGTH_SHORT).show();
//LocationText.setText("Your Location: Latitude " +locFromGps.getLatitude() + " Longitude: " +locFromGps.getLongitude());
double dbllatitude = locFromGps.getLatitude();
double dbllongitude = locFromGps.getLongitude();
double dblaltitude = locFromGps.getAltitude();
float bearing = locFromGps.getBearing();
KestrelActivity.LocationText.setText("Your Location: Latitude " + dbllatitude + " Longitude: " +dbllongitude + " Altitude " + dblaltitude + " Bearing: " + bearing);
hasbearing = locFromGps.hasBearing();
if (hasbearing = false) {
//Toast.makeText(getApplicationContext(), "No bearing", Toast.LENGTH_SHORT).show();
}
else
{
//Toast.makeText(getApplicationContext(), "I HAZ bearing: " + locFromGps.getBearing(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onProviderDisabled(String provider) {
// called when the GPS provider is turned off (user turning off the GPS on the phone)
}
@Override
public void onProviderEnabled(String provider) {
// called when the GPS provider is turned on (user turning on the GPS on the phone)
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// called when the status of the GPS provider changes
}
}
私の場所
package com.nick.kestrel;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Point;
import android.location.Location;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
public class MyLocation extends MyLocationOverlay {
private Context mContext;
static float mOrientation;
public MyLocation(Context context, MapView mapView) {
super(context, mapView);
mContext = context;
}
@Override
protected void drawMyLocation(Canvas canvas, MapView mapView, Location lastFix, GeoPoint myLocation, long when) {
// translate the GeoPoint to screen pixels
Point screenPts = mapView.getProjection().toPixels(myLocation, null);
// create a rotated copy of the marker
Bitmap arrowBitmap = BitmapFactory.decodeResource( mContext.getResources(), R.drawable.pin_icon);
Matrix matrix = new Matrix();
matrix.postRotate(mOrientation);
Bitmap rotatedBmp = Bitmap.createBitmap(
arrowBitmap,
0, 0,
arrowBitmap.getWidth(),
arrowBitmap.getHeight(),
matrix,
true
);
// add the rotated marker to the canvas
canvas.drawBitmap(
rotatedBmp,
screenPts.x - (rotatedBmp.getWidth() / 2),
screenPts.y - (rotatedBmp.getHeight() / 2),
null
);
mapView.postInvalidate();
}
public void setOrientation(float newOrientation) {
mOrientation = newOrientation;
}
}