我正在尝试从Android开发人员HelloGoogleMaps教程中编辑google地图,并结合locationlistener和locationmanager,以便将覆盖项目放置在我当前位置的地理位置(纬度,经度)。我是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>
你能否给我一个我需要的许可码? &GT;。&LT;
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}
答案 0 :(得分:0)
好吧,有一件事看起来你在初始化之前尝试访问位置变量,因为你的lon / lat和位置变量是类范围的,它们将在实际初始化位置之前被初始化(如果它是)
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是Google地图的内置叠加层,可以为您处理所有这些内容。
<强>更新强> 将位置var放在顶部附近
更改
double lat = location.getLatitude();
double lng = location.getLongitude();
到
double lat = 0;
double lng = 0;
然后在你的位置监听器中放下双,你只是创建一个本地,如果你这样做。
lat = location.getLatitude();
lng = location.getLongitude();
至少会处理这个问题,我会再次查看MyLocationOverlay的链接,它会为您提供有关当前位置的所有内容