2012-09-27 3 views
0

다음 소스 코드가 있으며 locationListener를 사용할 때지도에서 압정을받지 못합니다. 그러나, 위치 수신기를 사용하지 않으면 모든 것이 완벽하게 작동합니다. 다음과 같은android 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 및 인터넷을

아직도 그 테스트에 대한 문제가 무엇인지 잘 모릅니다 문제가되지 않는 GPS, 전화, 대상 구축

+1

안녕 필요 전화 설정, 위치 및 보안? – Luis

+0

예이 할 나는 보안상의 의미가 확실하지 않습니다. 거기에 GPS와 관련된 것을 찾지 못했습니다 ... – user1582256

+1

홈 화면에서 전화 버튼을 누르고 설정을 선택한 다음 위치 및 보안을 선택하면 표시해야 할 GPS 위성 사용 확인란이 표시됩니다 당신을 위해 전화 GPS가 작동합니다. – Luis

답변

0

질문에 대한 설명에서 ib elieve 당신은 그러나 여전히 다음과 같이이다, 당신의 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> 

을 두 GPS 권한을 준 당신은 GPS가 활성화 할 다음과 같이 두 개의 더 허가

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

해결책이 아닌 것 같지만 불행히도이 문제는 여전히 남아있다. (( – user1582256

관련 문제