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 和 INTERNET

仍然不知道在手机上进行测试的问题是什么。肯定不是问题 GPS,手机,构建目标

4

1 回答 1

0

根据您的问题描述,我相信您在您的 中只授予了两个 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>

但是,您仍然需要另外两个权限,如下所示,

<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 回答