簡體   English   中英

在仿真器上工作但不在真正的Android設備上

[英]Working on Emulator but not on the real Android device

我制作了一個Android應用程序,它在Android Screen的上半部分顯示了一個Google Map ,在Android Screen的下半部分顯示了一個TextView。

在我的Google Maps(on the Top half of the android screen) ,我在當前位置繪制一個Circle ,我DDMS perspective in the emulatorDDMS perspective in the emulator傳遞我current location (lat and long values) 它在模擬器中運行良好。

當我在模擬器上啟動應用程序時,首先它在Android屏幕的上半部分顯示了一個Google Map,在下半部分顯示了一個TextView。 然后使用DDMS Perspective,我傳遞了我當前的位置(lat和long值),並且在傳遞之后,它在Google地圖上的當前位置繪制了一個Circle。

一切都在模擬器中正常工作。

問題陳述:-

但是當我在真正的Android Phone上嘗試同樣的事情時,只有我的Google Maps在Android屏幕的上半部分和Android屏幕下半部分的TextView上正確顯示。 但它並沒有Circle on my Current Location繪制Circle on my Current Location

我在Android手機上連接了Wi-Fi。 我不這么認為我需要在Android手機上傳遞緯度和經度值,對嗎? 它應該自動采取當前位置嗎?

或者我的Android手機上有什么需要做的嗎?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

    locationListener = new GPSLocationListener();

    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            locationListener);

    mapView = (MapView) findViewById(R.id.mapView);
    listView = (ListView) findViewById(R.id.mylist);
    mapView.setStreetView(true);
    mapView.setBuiltInZoomControls(true);

    mapController = mapView.getController();
    mapController.setZoom(15);
}

private class GPSLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            GeoPoint point = new GeoPoint(
                    (int) (location.getLatitude() * 1E6), 
                    (int) (location.getLongitude() * 1E6));

            findUsersInCurrentRadius(4,location.getLatitude(),location.getLongitude());

            mapController.animateTo(point);
            mapController.setZoom(15);

            // add marker
            MapOverlay mapOverlay = new MapOverlay(this,android.R.drawable.star_on,R.drawable.tenm,R.drawable.twentym,R.drawable.thirtym,R.drawable.fourtym);
            mapOverlay.setPointToDraw(point);
            List<Overlay> listOfOverlays = mapView.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(mapOverlay);

            String address = ConvertPointToLocation(point);
            Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();

            mapView.invalidate();
        }
    }


class MapOverlay extends Overlay {
    private GeoPoint pointToDraw;
    int[] imageNames=new int[6];

    public MapOverlay(GPSLocationListener gpsLocationListener,
            int currentUser, int tenm, int twentym, int thirtym, int fourtym) {
        imageNames[0]=currentUser;
        imageNames[1]=tenm;
        imageNames[2]=twentym;
        imageNames[3]=thirtym;
        imageNames[4]=fourtym;
    }

    public void setPointToDraw(GeoPoint point) {
        pointToDraw = point;
    }

    public GeoPoint getPointToDraw() {
        return pointToDraw;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
        super.draw(canvas, mapView, shadow);
        //---translate the GeoPoint to screen pixels---
        Point screenPts = new Point();
        mapView.getProjection().toPixels(pointToDraw, screenPts);
        //--------------draw circle----------------------
        Point pt = mapView.getProjection().toPixels(pointToDraw,screenPts);
        Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(0x30000000);
        circlePaint.setStyle(Style.FILL_AND_STROKE);

        int totalCircle=4;
        int radius=40;
        int centerimagesize=35;

        for (int i = 1; i <= totalCircle; i ++) { 
            canvas.drawCircle(screenPts.x,screenPts.y, i*radius, circlePaint); 
        } 
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),imageNames[0]), (screenPts.x-(centerimagesize/2)),(screenPts.y-(centerimagesize/2)), null);
        super.draw(canvas,mapView,shadow);


        return true;
    }
} 

而我的AndroidManifest.xml文件 -

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.circlemapandroid"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="8" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:debuggable="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps" />

        <activity
            android:name=".ThesisProjectAndroid"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我可以從您的代碼中看到,只有在收到第一個修復后才會繪制圓圈。 您是否在手機設置中啟用了GPS(位置和安全性)? 如果沒有,請啟用它。

只有在從GPS獲得第一個修復后,您才能獲得圓圈。 您需要將GPS帶到室外才能獲得它。

你在位置監聽器中有一個Toast,所以如果你得到一個帶有地址值的Toast,以上都不會解決你的問題。

最后為了提高效率,我會移動代碼來創建並添加覆蓋范圍內的偵聽器,因為每次收到新修補程序時都會創建並刪除它。

您缺少GPS的另一個權限,請在AndroidManifest.xml中添加此權限

<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM