2011-07-26 36 views
0

我试图从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} 
+1

你在Eclipse这样做?你能从DDMS的角度发布Logcat的输出吗? – Estel

+0

哈哈,我不知道我应该使用哪个时间段... – Eric

+0

在你的logcat上,你可以按下“E”按钮来只获得错误,这通常是有帮助的,控制台也可以显示堆栈跟踪打印输出,这假定你正在使用eclipse – Idistic

回答

0

好一两件事,它看起来像你想它初始化之前访问该位置的变量,因为你经度/纬度和位置瓦尔是此类范围内的位置实际上是初始化之前他们将得到初始化(如果它曾经是)

double lat = location.getLatitude(); // location is never set, it will be null! 
double lng = location.getLongitude(); 

由于事实上我甚至不看你初始化位置(你有你的更新同名的参数变量,但我看不出你是移动的类别范围版本)

public void onLocationChanged(Location location) {/

即使你不喜欢的东西

this.location = new Location(location); 

里面的听众,你不知道什么时候这将创建(或即使)

而且MyLocationOverlay是一个内置的叠加谷歌地图是将为您处理所有这些。

更新 跌落的位置VAR顶部

变化附近

double lat = location.getLatitude(); 
double lng = location.getLongitude(); 

double lat = 0; 
double lng = 0; 

然后里面你的位置监听器掉落双倍,你只是在创建本地如果你这样做。

lat = location.getLatitude(); 
lng = location.getLongitude(); 

至少会照顾这个问题的,我再次看到的链接MyLocationOverlay,它需要照顾的一切对你的问候当前位置

+0

你是什么意思的类作用域? – Eric

+0

我的意思是这个位置的位置;在类的顶部,并且可用于任何方法,与此public void onLocationChanged(Location location)不同(仅在位置侦听器中可用)。您尝试使用该类的范围之前,它实际上initalized - 我更新了上面的答案 – Idistic

+0

public void onLocationChanged(Location location){ double lat = location.getLatitude(); double lng = location.getLongitude(); } – Eric