0

次のソース コードがあり、locationListener を使用すると、地図上に画鋲が表示されません。ただし、ロケーションリスナーを使用していない場合、すべてが完全に機能します。あなたの助けが必要です

package com.example.googlemaps;

import java.util.Iterator;
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.MapView.LayoutParams;
import com.google.android.maps.OverlayItem;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;


public class MapsActivity extends MapActivity {
    MapView mapView;
    //locationManager provides capability to receive info from GPS
    LocationManager locManager;
    LocationListener locListener;
    GeoPoint p;

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

        //zoom feature is build in MapView class
        mapView = (MapView)findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);

        initLocationManager();
    }

    /**
     * initialize the locationManager
     */

    private void initLocationManager(){
        locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locListener = new LocationListener() {

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub
                // called when the status of the GPS provider changes
            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub
                // called when the GPS provider is turned on (user turning on the GPS on the phone)
            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
                // called when the GPS provider is turned off (user turning off the GPS on the phone)
            }

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                // called when the listener is notified with a location update from the GPS
                createAndShowMyItemizedOverlay(location);
            }
        };
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locListener);
    }

    /**
     * this method will be called whenever a change of current position is
     * submitted by GPS
     */
    private void createAndShowMyItemizedOverlay(Location newLocation){
        List<Overlay> listOverlays = mapView.getOverlays();
        listOverlays.clear();

        //initialize icon
        Drawable icon = this.getResources().getDrawable(R.drawable.pushpin);

        //transform the location to a geopoint
                p = new GeoPoint((int)(newLocation.getLatitude()*1E6), (int)(newLocation.getLongitude()*1E6));

        //create the overlay and show it
        MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(icon, this);
        OverlayItem overlayItem = new OverlayItem(p, "My Location", "I am in Dublin");

        itemizedOverlay.addItem(overlayItem);
        listOverlays.add(itemizedOverlay);
        mapView.getOverlays().add(itemizedOverlay);

        //move to location
        mapView.getController().animateTo(p);

        mapView.getController().setZoom(17);
        //redraw map
        mapView.postInvalidate();
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

および MyItemizedOverlay クラスは次のとおりです。

package com.example.googlemaps;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;


public class MyItemizedOverlay extends ItemizedOverlay{

    private ArrayList<OverlayItem> ListItems = new ArrayList<OverlayItem>();
    private Drawable marker;
    private Context mContext;

    public MyItemizedOverlay(Drawable defaultMarker) {
        // TODO Auto-generated constructor stub
        super(boundCenterBottom(defaultMarker));
    }

    public MyItemizedOverlay(Drawable defaultMarker, Context context){
        super(boundCenterBottom(defaultMarker));
        mContext = context;
    }

    /**
     * When the populate() method executes, it will call 
     * createItem(int) in the ItemizedOverlay to retrieve each OverlayItem. 
     * You must override this method to properly read from the ArrayList 
     * and return the OverlayItem from the position specified by the given integer. 
     */
    @Override
    protected OverlayItem createItem(int index) {
        // TODO Auto-generated method stub
        return ListItems.get(index);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return ListItems.size();
    }

    //addItem is used to add new OverlayItem object to ArrayList
    public void addItem(OverlayItem item) {
        ListItems.add(item);
        populate();
    }

    @Override
    protected boolean onTap(int index) {
        OverlayItem item = (OverlayItem) ListItems.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        return true;
    }
}

PS: マニフェストに ACCESS_FINE_LOCATION と ACCESS_COARSE_LOCATION AND INTERNET があります

電話でのテストに関する問題が何であるかはまだわかりません。GPS、電話、ビルドターゲットに問題がないことは確かです

4

1 に答える 1

0

あなたの質問の説明から、次の 2 つの GPS 許可のみを に与えていると思いますAndroidManifest.xml

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

ただし、次のように、さらに 2 つの権限が必要です。

<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission>
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"></uses-permission>
于 2012-09-28T03:23:30.037 に答える