1

Google Mapを上半分にAndroid Screen表示し、Android画面の下半分にTextViewを表示するAndroidアプリケーションを作成しました。

そして、私は現在の場所Google Maps(on the Top half of the android screen)に描画しており、からCircleパスしています。そして、それはエミュレーターでうまく機能しています。current location (lat and long values)DDMS perspective in the emulator

エミュレーターでアプリケーションを起動すると、まずAndroid画面の上半分にGoogleマップが表示され、下半分にTextViewが表示されます。次に、DDMSパースペクティブを使用して、現在の場所(緯度と経度の値)を渡します。通過した後、Googleマップの現在の場所に円を描画します。

そして、エミュレーターではすべてが正常に機能します。

問題文:-

しかし、実際に同じことを試していると、Android画面の上半分にAndroid Phone自分だけGoogle Mapsが正しく表示され、Android画面の下半分にTextViewが表示されます。しかし、それは描画していません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>
4

2 に答える 2

2

あなたのコードから、最初の修正が受信された後にのみ円が描画されることがわかります。電話の設定 (位置情報とセキュリティ) で GPS を有効にしていますか? そうでない場合は、有効にします。

GPS から最初の修正を取得した後にのみ円を取得します。GPS を取得するには、屋外に持ち出す必要があります。

ロケーションリスナーにトーストがあるため、アドレスの値でトーストを取得している場合、上記のいずれも問題を解決しません。

最後に、新しい修正を受け取るたびにオーバーレイを作成して削除するのは重たいので、コードを移動してロケーション リスナーからオーバーレイを作成して追加します。

于 2012-09-16T23:26:22.497 に答える
1

GPSのもう1つの権限が不足している場合は、これをAndroidManifest.xmlに追加してください

<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission>
于 2012-09-16T01:09:26.843 に答える