1

次のコードを使用して、AndroidフォンでGoogle Maps APIを試しています。私のアプリは自分の電話を見つけてうまく機能していますが、GPS通知アイコンが点滅していません。電話を見つけるときにGPSアイコンを点滅させるにはどうすればよいですか.

返信ありがとうございます。

マニフェスト:

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

そしてコード:

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.EditText;
import android.widget.Toast;

public class main extends MapActivity {

    MapController mControl;
    GeoPoint geoP;
    MapView mapV;
    MyLocationOverlay compass;
    EditText etext;
    String provider;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        etext = (EditText) findViewById(R.id.latLong);
        mapV = (MapView) findViewById(R.id.mapView);
        mapV.displayZoomControls(true);
        mapV.setBuiltInZoomControls(true);

        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        boolean enable = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (!enable) {
            startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
        }

        Criteria criteria = new Criteria();
        //criteria.setAccuracy(Criteria.ACCURACY_FINE);
        provider = locationManager.getBestProvider(criteria, false);

        Location location = locationManager.getLastKnownLocation(provider);
        Double lat = location.getLatitude() * 1E6;
        Double longi = location.getLongitude() * 1E6;
        geoP = new GeoPoint(lat.intValue(), longi.intValue());

        List<Overlay> mapOverlays = mapV.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
        compass = new MyLocationOverlay(this, mapV);

        HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
        OverlayItem overlayitem = new OverlayItem(geoP, "Hola, Mundo!","I'm in Mexico City!");

        itemizedoverlay.addOverlay(overlayitem);

        mapV.getOverlays().add(compass);
        mapOverlays.add(itemizedoverlay);

        mControl = mapV.getController();
        mControl.animateTo(geoP);
        mControl.setZoom(5);
        mControl.setCenter(geoP);

        locationManager.requestLocationUpdates(provider, 0, 0,
                new LocationListener() {

                    public void onStatusChanged(String provider, int status,
                            Bundle extras) {
                        if (status == LocationProvider.AVAILABLE) {
                            Toast.makeText(getApplicationContext(),
                                    "LocationProvider.AVAILABLE",
                                    Toast.LENGTH_SHORT).show();
                        } else if (status == LocationProvider.TEMPORARILY_UNAVAILABLE) {
                            Toast.makeText(getApplicationContext(),
                                    "LocationProvider.TEMPORARILY_UNAVAILABLE",
                                    Toast.LENGTH_SHORT).show();
                        } else if (status == LocationProvider.OUT_OF_SERVICE) {
                            Toast.makeText(getApplicationContext(),
                                    "LocationProvider.OUT_OF_SERVICE",
                                    Toast.LENGTH_SHORT).show();
                        }

                    }

                    public void onProviderEnabled(String provider) {

                    }

                    public void onProviderDisabled(String provider) {

                    }

                    public void onLocationChanged(Location location) {
                        Toast.makeText(getApplicationContext(),
                                "Location Changed", Toast.LENGTH_SHORT).show();

                    }
                });

        //etext.setText(String.valueOf(lat) + "," + String.valueOf(longi));

    }

    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        compass.disableCompass();
    }

    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        compass.enableCompass();
    }

    protected boolean isRouteDisplayed() {
        return false;
    }
4

1 に答える 1

1

t0mm13b、ハンドセットで動作していますが、GPS 通知アイコンが点滅しません。仮想デバイスとは、エミュレーターを意味します。問題を解決しました。

コード :

package com.yunusoksuz.gmapstest;

import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class YnsmapsActivity extends Activity implements LocationListener {
    /** Called when the activity is first created. */
    private LocationManager locationManager;
    private Location location;
    private String provider;
    private Double lat, longi;
    private Criteria criteria;
    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.tv1);
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        provider = locationManager.getBestProvider(criteria, false);
        location = locationManager.getLastKnownLocation(provider);
        locationManager.requestLocationUpdates(provider, 0, 0, this);

    }

    public void onLocationChanged(Location location) {
        Toast.makeText(
                this,
                "Lat : " + location.getLatitude() + " LONG : "
                        + location.getLongitude(), Toast.LENGTH_SHORT).show();
        tv.setText("Lat : " + location.getLatitude() + " LONG : "
                + location.getLongitude());
    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        locationManager.removeUpdates(this);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, this);
    }
}

GPS通知が点滅するようになりました。

于 2012-07-16T20:52:56.563 に答える