Android getbearing 仅返回 0.0.尝试使用旋转当前位置图标 [英] Android getbearing only returns 0.0. Trying to use to rotate current location icon

查看:12
本文介绍了Android getbearing 仅返回 0.0.尝试使用旋转当前位置图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这部分是对上一个问题的扩展:Android编程,mapview locationoverlay:如何改变默认的蓝色位置点,用另一张图片

This is in part an extension to a previous question: Android programming, mapview locationoverlay: how to change the default blue location dot, with another image

我一直在努力让设备位置显示在地图上(使用 enablemylocation 很容易),但我需要替换默认图标(很简单,对其进行排序)然后旋转它以对应于当前设备方位 - 获得来自 GPS,因为它需要是移动方向,而不是手机面对的方向.

I have been working to get the devices location to display on the map (easy with enablemylocation) but I need to replace the default icon (easy enough, sorted that) and then rotate it to correspond to the current device bearing - acquired from GPS, as it needs to be direction of movement, not direction the phone is facing.

我使用 'getbearing()' 的方式与成功使用 'getlatitude()' 和 'getaltitude()' 的方式相同,但 getbearing 总是返回 0.(以确保它不是代码旋转的图标是问题轴承的变量在发生其他任何事情之前打印在文本视图中)

I am using 'getbearing()' in the same way as I've been successfully using 'getlatitude()' and 'getaltitude()', but getbearing always returns 0. (To make sure it wasn't the code that rotates the icon that was the problem the variable for the bearing is printed in a textview before anything else happens)

非常奇怪的是,它现在在 5 秒内工作了两次,然后又返回 0 - 所以理论上代码看起来是正确的?

The very peculiar thing is that it has worked twice now for all of 5 seconds, then gone back to returning 0 - so in theory the code seems correct?

代码如下.

亲切的问候

尼克

主要活动:

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

}

推荐答案

问题解决了!感谢 Aleksejs 的努力 - 原来这是一个 PICNIC 的案例.

Problem solved! Thanks to Aleksejs for your efforts - turned out it was a case of PICNIC.

我已将 GPS 设置为以最小的移动进行更新,并且随着位置的跳跃,我认为它应该能够计算出方位并继续前进.

I had set the GPS to update at the smallest movement, and as the location was jumping around I assumed it should be able to work out bearing and get on with it.

在测试 'getSpeed()' 的结果时,我注意到轴承工作正常 - 结果你必须比花园周围移动一点,以便有时间计算你的轴承.

When testing out the result from 'getSpeed()' I noticed bearing working - turns out you have to move a little further than around the garden to give it time to work out your bearing.

问候

尼克

这篇关于Android getbearing 仅返回 0.0.尝试使用旋转当前位置图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆