安卓getbearing只返回0.0。试图用旋转的当前位置图标当前位置、图标、试图用、getbearing

2023-09-07 02:18:17 作者:不浪漫罪名

这是部分延伸到previous问题:Android编程,图形页面locationoverlay:如何更改默认的蓝色位置点,与其他图片

我一直在努力来获得设备的位置显示在地图(易与enablemylocation)上,但我需要替换默认的图标(很容易,排序的),然后将其旋转至对应于当前设备轴承 - 收购从全球定位系统,因为它需要的运动方向,而不是方向上的电话面临。

我使用的,因为我已经成功地使用'getlatitude()'和'getaltitude()''getbearing()'以同样的方式,但总是getbearing返回0(以确保它不是code表示旋转,这是该变量为轴承是印在一个TextView之前,什么都发生)问题的图标

非常奇特的是,它目前已经摸索两次,所有的5秒钟,然后又回到了久违0 - 所以理论上code似乎是正确的。

code以下。

亲切的问候

尼克

Mainactivity:

 包com.nick.kestrel;

进口android.content.Context;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.widget.TextView;

进口com.google.android.maps.MapActivity;
进口com.google.android.maps.MapView;


公共类KestrelActivity扩展MapActivity {
/ **第一次创建活动时调用。 * /

静态的TextView LocationText;
图形页面图形页面;


@覆盖
保护的布尔isRouteDisplayed(){
        返回false;
}

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

  //标识的TextViewlocationtext作为变量LocationText。只是为了调试规划用途
    LocationText =(TextView中)findViewById(R.id.locationtext);

  //定义的MapView作为变量图形页面,使缩放控制
    图形页面=(图形页面)findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(真正的);

    / **
     * code接收GPS位置所需。激活的GPS提供商,并且仅设置后更新
     *为至少10秒,至少为10米的位置变化
     * /
    LocationListener的LocationListener的=新MyLocationListener();

    //设置位置管理
    LocationManager locman =(LocationManager)getSystemService(Context.LOCATION_SERVICE);

    locman.requestLocationUpdates(LocationManager.GPS_PROVIDER,500(浮点)0.01,LocationListener的);

    //添加当前位置叠加到地图图形页面,并打开地图上的指南针

    MyLocation myLocationOverlay =新MyLocation(本,图形页面);
    myLocationOverlay.enableMyLocation();
    myLocationOverlay.enableCompass();

    。调用MapView.getOverlays()加(myLocationOverlay);
    mapView.postInvalidate();
}
 

}

mylocationlistener

 包com.nick.kestrel;

进口android.location.Location;
进口android.location.LocationListener;
进口android.os.Bundle;


公共类MyLocationListener实现LocationListener的{

私人布尔hasbearing;

/ **
 * code当侦听器能够接收一个新的位置运行
 * /



@覆盖
公共无效onLocationChanged(位置locFromGps){



    //Toast.makeText(getApplicationContext(),位置变化,纬度:+ locFromGps.getLatitude()+长:+ locFromGps.getLongitude(),Toast.LENGTH_SHORT).show();

    //LocationText.setText("Your地点:纬度+ locFromGps.getLatitude()+经度:+ locFromGps.getLongitude());

    双dbllatitude = locFromGps.getLatitude();
    双dbllongitude = locFromGps.getLongitude();
    双dblaltitude = locFromGps.getAltitude();
    浮动轴承= locFromGps.getBearing();

    KestrelActivity.LocationText.setText(您的位置:纬度+ dbllatitude +经度:+ dbllongitude +海拔高度+ dblaltitude +轴承+轴承);



    hasbearing = locFromGps.hasBearing();


    如果(hasbearing = FALSE){

        //Toast.makeText(getApplicationContext(),没有关系,Toast.LENGTH_SHORT).show();

    }
    其他
    {
        //Toast.makeText(getApplicationContext(),我H​​AZ轴承:+ locFromGps.getBearing(),Toast.LENGTH_SHORT).show();
    }


}

@覆盖
公共无效onProviderDisabled(字符串提供商){
   //当GPS供应商关闭被称为(用户关闭手机上的GPS)
}

@覆盖
公共无效onProviderEnabled(字符串提供商){
   //调用当GPS提供商被接通(用户接通GPS电话)
}

@覆盖
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
   //调用时的GPS供应商的状态发生变化

}
 
DGUS组态软件中图标旋转显示如何使用

}

mylocation

 包com.nick.kestrel;

进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Canvas;
进口android.graphics.Matrix;
进口android.graphics.Point;
进口android.location.Location;

进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.MapView;
进口com.google.android.maps.MyLocationOverlay;



公共类MyLocation扩展MyLocationOverlay {
私人语境mContext;
静浮mOrientation;

公共MyLocation(上下文的背景下,图形页面图形页面){
    超(背景下,图形页面);
    mContext =背景;
}

@覆盖
保护无效drawMyLocation(画布油画,图形页面图形页面,地点lastFix,GeoPoint对象myLocation,时长)​​{
    //翻译的GeoPoint到屏幕像素
    点screenPts = MapView.getProjection()在toPixels(myLocation,空)。

    //创建标记的循环副本
    位图arrowBitmap = BitmapFactory.de codeResource(mContext.getResources(),R.drawable.pin_icon);
    字模=新的Matrix();
    matrix.postRotate(mOrientation);
    位图rotatedBmp = Bitmap.createBitmap(
        arrowBitmap,
        0,0,
        arrowBitmap.getWidth(),
        arrowBitmap.getHeight(),
        矩阵,
        真正
    );
    //添加旋转标记到画布
    canvas.drawBitmap(
        rotatedBmp,
        screenPts.x  - (rotatedBmp.getWidth()/ 2),
        screenPts.y  - (rotatedBmp.getHeight()/ 2),

        空值

    );
    mapView.postInvalidate();

}

公共无效setOrientation(浮动newOrientation){
     mOrientation = newOrientation;
}
 

}

解决方案

问题解决了!感谢Aleksejs你的努力 - 原来,这是野餐的情况下

我所设定的GPS更新的最小运动,作为位置是跳来跳去,我认为它应该能够制定出轴承和得到它。

在测试出来的结果,从getSpeed​​()我注意到了轴承的工作 - 原来您要搬到远一点在花园里,而不是给它时间来解决你的轴承

问候

尼克

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

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.

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)

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?

Code follows.

Kind regards

Nick

Mainactivity:

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

}

mylocationlistener

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

}

}

mylocation

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

}

解决方案

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

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.

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.

Regards

Nick