2012-04-21 94 views
0

這部分的擴展,以前的問題:Android programming, mapview locationoverlay: how to change the default blue location dot, with another imageAndroid getbearing僅返回0.0。嘗試使用旋轉當前位置圖標

我一直在努力來獲得設備的位置顯示在地圖(容易enablemylocation)上,但我需要更換默認的圖標(很簡單,按順序排列),然後旋轉它以對應於當前設備的方位 - 從GPS獲取,因爲它需要是移動方向,而不是手機朝向的方向。

我正在使用'getbearing()',就像我已經成功使用'getlatitude()'和'getaltitude()'一樣,但getbearing總是返回0.(爲了確保它不是代碼旋轉圖標,這是問題的方法變量在文本視圖中打印之前發生任何其他事情)

非常奇怪的是,它已經工作了兩次現在所有的5秒,然後回去返回0 - 理論上這個代碼看起來正確嗎?

代碼如下。

親切的問候

尼克

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

}

回答

0

問題解決了!感謝Aleksejs的努力 - 事實證明這是一個PICNIC的案例。

我已經將GPS設置爲以最小的移動進行更新,並且由於位置在四處跳動,我認爲它應該能夠解決軸承問題並繼續進行。

當檢測出從結果「getSpeed()」我注意到軸承的工作 - 原來你要搬到遠一點周圍的花園,而不是給它時間來解決您的軸承。

問候

尼克

0

什麼是地點的供應商?如果它NETWORK_PROVIDER,那麼你不會承受,因爲它的準確性很低,檢測軸承。

試試你的應用程序外,並與GPS_PROVIDER

+0

見外景經理的主要活動 - 使用GPS_PROVIDER。 我目前可以獲得精確的GPS結果,並​​在窗口頂部的文本視圖中打印,並顯示海拔高度和方位。軸承是唯一返回0.0的結果... – Nick 2012-04-21 11:57:17

+0

問題已解決。只要系統允許,系統會盡快發佈答案。 - PICNIC – Nick 2012-04-21 12:17:08

相關問題