我試圖從Android開發者HelloGoogleMaps教程結合locationlistener和locationmanager編輯google地圖,這樣overlay項目將被放置在我當前位置的地理點(經度,緯度)。我是一個在Android的極端初學者,所以請用簡單的語言來幫助我!的xD爲什麼這個地圖程序「意外停止」?
我SampleMap.java文件:
package com.example.samplemap;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class SampleMap extends MapActivity {
/** Called when the activity is first created. */
Location location;
@Override
protected boolean isRouteDisplayed() {
return false;
}
double lat = location.getLatitude();
double lng = location.getLongitude();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
GeoPoint point = new GeoPoint((int) (lat), (int) (lng));
OverlayItem overlayitem = new OverlayItem(point, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
private class mylocationlistener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
Toast.makeText(SampleMap.this,
location.getLatitude() + " " + location.getLongitude(),
Toast.LENGTH_LONG).show();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
和我HelloItemizedOverly.java
package com.example.samplemap;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
Context mContext = null;
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
和清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.samplemap"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"> </uses- permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".SampleMap"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
你能不能也給我許可的代碼,我需要??? >。 <
DDMS log cat:
07-26 13:53:27.077: DEBUG/EAS SyncManager(2258): !!! EAS SyncManager, onCreate
07-26 13:53:27.171: DEBUG/Eas Debug(2258): Logging:
07-26 13:53:27.179: DEBUG/EAS SyncManager(2258): !!! EAS SyncManager, onDestroy
07-26 13:53:28.296: DEBUG/dalvikvm(2258): GC_FOR_MALLOC freed 3927 objects/265008 bytes in 44ms
07-26 13:53:28.890: DEBUG/dalvikvm(1699): GC_EXPLICIT freed 265 objects/15568 bytes in 86ms
07-26 13:53:29.429: DEBUG/Email(2258): *** synchronizeMailboxGeneric ***
07-26 13:53:30.398: INFO/ActivityManager(1103): No longer want com.google.android.apps.uploader (pid 2084): hidden #16
07-26 13:53:30.421: INFO/UsageStats(1103): Something wrong here, didn't expect com.example.samplemap to be paused
07-26 13:53:30.925: WARN/ActivityManager(1103): Activity pause timeout for HistoryRecord{44a2a790 com.example.samplemap/.SampleMap}
07-26 13:53:33.882: DEBUG/dalvikvm(1187): GC_EXPLICIT freed 1914 objects/112528 bytes in 75ms
07-26 13:53:38.874: DEBUG/dalvikvm(2011): GC_EXPLICIT freed 91 objects/8040 bytes in 69ms
07-26 13:53:40.426: WARN/ActivityManager(1103): Activity idle timeout for HistoryRecord{44a2a790 com.example.samplemap/.SampleMap}
07-26 13:53:45.499: DEBUG/dalvikvm(2019): GC_EXPLICIT freed 304 objects/17824 bytes in 76ms
07-26 13:53:50.491: DEBUG/dalvikvm(2074): GC_EXPLICIT freed 135 objects/6720 bytes in 66ms
07-26 13:53:55.507: DEBUG/dalvikvm(1560): GC_EXPLICIT freed 65 objects/3896 bytes in 75ms
07-26 13:54:05.132: DEBUG/dalvikvm(1726): GC_EXPLICIT freed 46 objects/2216 bytes in 54ms
07-26 13:54:10.171: DEBUG/dalvikvm(1445): GC_EXPLICIT freed 268 objects/9960 bytes in 94ms
07-26 13:54:15.156: DEBUG/dalvikvm(1215): GC_EXPLICIT freed 1011 objects/51888 bytes in 75ms
07-26 13:54:16.804: DEBUG/KeyguardViewMediator(1103): wakeWhenReadyLocked(26)
07-26 13:54:16.804: DEBUG/KeyguardViewMediator(1103): handleWakeWhenReady(26)
07-26 13:54:16.804: DEBUG/KeyguardViewMediator(1103): pokeWakelock(5000)
07-26 13:54:16.804: INFO/power(1103): *** set_screen_state 1
07-26 13:54:16.829: DEBUG/Sensors(1103): using sensors (name=sensors)
07-26 13:54:16.851: INFO/UsageStats(1103): Unexpected resume of com.example.samplemap while already resumed in com.example.samplemap
07-26 13:54:16.882: DEBUG/WifiService(1103): ACTION_SCREEN_ON
07-26 13:54:17.124: DEBUG/dalvikvm(1184): GC_FOR_MALLOC freed 11174 objects/598888 bytes in 105ms
07-26 13:54:17.382: DEBUG/SurfaceFlinger(1103): Screen about to return, flinger = 0x120f38
07-26 13:54:19.085: DEBUG/WindowManager(1103): I'm tired mEndcallBehavior=0x2
07-26 13:54:19.328: INFO/power(1103): *** set_screen_state 0
07-26 13:54:19.341: DEBUG/SurfaceFlinger(1103): About to give-up screen, flinger = 0x120f38
07-26 13:54:19.359: DEBUG/Sensors(1103): using accelerometer (name=accelerometer)
07-26 13:54:19.374: DEBUG/WifiService(1103): ACTION_SCREEN_OFF
07-26 13:54:19.382: DEBUG/WifiService(1103): setting ACTION_DEVICE_IDLE timer for 900000ms
07-26 13:54:19.870: WARN/ActivityManager(1103): Activity pause timeout for HistoryRecord{44a2a790 com.example.samplemap/.SampleMap}
你在Eclipse這樣做?你能從DDMS的角度發佈Logcat的輸出嗎? – Estel
哈哈,我不知道我應該使用哪個時間段... – Eric
在你的logcat上,你可以按下「E」按鈕來只獲得錯誤,這通常是有幫助的,控制檯也可以顯示堆棧跟蹤打印輸出,這假定你正在使用eclipse – Idistic