2012-04-27 86 views
1

我正在研究一個項目,該項目具有顯示(動畫)當前用戶位置和一個硬編碼位置(給定經度和緯度)的功能。到目前爲止,我有硬編碼的位置工作,但我無法弄清楚如何獲得用戶位置。我試過位置偵聽器,但是我的應用程序在運行後不斷崩潰。有人對此有任何建議嗎?非常感激。下面的代碼:Animate to User Location(Android)

public class LocationsActivity extends MapActivity implements LocationListener { 

MapView mapView; 
MapController mc; 
GeoPoint p; 
LocationListener locListener; 



public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.location); 

    mapView = (MapView)findViewById(R.id.mapView); 
    mapView.setBuiltInZoomControls(true); 

    mc = mapView.getController(); 
    String coordinates[] = {"52.67596","-8.64910"}; 
    double lat = Double.parseDouble(coordinates[0]); 
    double lng = Double.parseDouble(coordinates[1]); 

    p = new GeoPoint((int)(lat*1E6),(int)(lng*1E6)); 
    OverlayItem overlayitem = new OverlayItem(p, "Limerick Institute of 
     Technology", "Moylish"); 




    mc.animateTo(p); 
    mc.setZoom(17); 

    List<Overlay> mapOverlays = mapView.getOverlays(); 
    Drawable drawable = this.getResources().getDrawable(R.drawable.pushpin); 
    HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, 
     this); 
    itemizedoverlay.addOverlay(overlayitem); 
    mapOverlays.add(itemizedoverlay); 
    mapView.invalidate(); 
} 


@Override 
protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
} 


public void onLocationChanged(Location arg0) { 
    // TODO Auto-generated method stub 

} 


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 

} 


} 
+0

讓您的解決方案from here..http://android-er.blogspot.in/2009/11/mapview-to-center-on-current-location.html – Bhavin 2012-04-27 13:17:12

+0

please a dd崩潰日誌.. – Ronnie 2012-04-27 13:23:17

+0

@ userSeven7s它就像IllegalArgumentException:listener == null。我認爲這與事實有關,如果你更新當前位置,它可能返回一個空值。 – 2012-04-27 13:29:25

回答

0

我建立在ICS應用程序有一個片段中一個MapView。 在活動類中初始化mapView之後,我將它傳遞給mapFragment,它是用於顯示地圖的片段。

這是一些我的活動類代碼:

mapView = new MapView(this, getString(R.string.mapsAPI)); 

myLocationOverlay = new MyLocationOverlay(this, mapView); 
mapView.getOverlays().add(this.myLocationOverlay); 
mapView.postInvalidate(); 

public void calculateLocation() { 
setLocationManager(); 
setLocation(); 
setGeoPoint(location); 
} 

public void setGeoPoint(Location location) { 
    double latitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 
    geoPoint = new GeoPoint((int)(latitude * 1E6), (int)(longitude * 1E6)); 
} 

public void setLocation() { 
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
} 

public void setLocationManager() { 
locationManager = ((LocationManager)getSystemService(Context.LOCATION_SERVICE)); 
} 

,這是我mapFragment類代碼:

public class MapFragment extends Fragment 
{ 
    MapView gMap = null; 
    GeoPoint gPoint = null; 
    Location location = null; 

    public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle) { 
    gMap = ((NFCDemoActivity)getActivity()).getMapView(); 
    return this.gMap; 
    } 

    public void onActivityCreated(Bundle paramBundle) { 
    super.onActivityCreated(paramBundle); 
    ((NFCDemoActivity)getActivity()).calculateLocation(); 
    gPoint = ((NFCDemoActivity)getActivity()).getGeoPoint(); 
    gMap.setClickable(true); 
    gMap.setBuiltInZoomControls(true); 
    gMap.getController().setZoom(18); 
    gMap.getController().setCenter(gPoint); 
    } 

    public void onDestroy() { 
    super.onDestroy(); 
    } 
} 

這會給你的用戶的當前位置。

3

如果你正在使用MapActivity那麼你可以使用下面的代碼:如果您正在使用MapFragment然後活動中

/** 
* Set current location using MyLocationOverlay 
*/ 
public void setCurrentLocation() { 

    myLocationOverlay.runOnFirstFix(new Runnable() { 
     @Override 
     public void run() { 

      if (null != myLocationOverlay.getMyLocation()) { 

       /** 
       * Put extra sleep to avoid concurrentModicationException 
       */ 
       try { 
        Thread.sleep(1000); 

       } catch (Exception e) { 
        // TODO: handle exception 
       } 
       map.getController().animateTo(
         myLocationOverlay.getMyLocation()); 

       map.getOverlays().add(myLocationOverlay); 

      } 

     } 
    }); 

} 

1. mapFragment.getMap().setMyLocationEnabled(true); 

然後調用

private void moveMapToMyLocation() { 

    LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    Criteria crit = new Criteria(); 

    Location loc = locMan.getLastKnownLocation(locMan.getBestProvider(crit, 
      false)); 

    CameraPosition camPos = new CameraPosition.Builder() 

    .target(new LatLng(loc.getLatitude(), loc.getLongitude())) 

    .zoom(12.8f) 

    .build(); 

    CameraUpdate camUpdate = CameraUpdateFactory.newCameraPosition(camPos); 

    mapFragment.getMap().moveCamera(camUpdate); 

} 
+0

是的。這只是爲了演示的目的。 但反正使用MapAcivity是現在被刪除的方式。 – 2014-05-12 09:11:58