gpt4 book ai didi

android - 如何在 android 上添加叠加层以映射?

转载 作者:行者123 更新时间:2023-11-29 21:52:55 25 4
gpt4 key购买 nike

我尝试应用此 link 中的这些代码.但是我遇到了像 NullPointerException 这样的错误。

我使用 fragment ,这是我的 BaseFragment 中的函数

 public void createMapViewForHaritaFragment(IlanList list)
{
IlanList gelenListe;
gelenListe=list;

if(gelenListe!=null)
{
MainActivity parent = getParentActivity();
mMapViewContainer = parent.getmMapViewContainer();
mMapView = parent.getmMapView();

/**/
mc = mMapView.getController();

LocationManager lm = (LocationManager) parent.getSystemService(parent.getApplicationContext().LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
String longitude = String.valueOf(location.getLongitude()).substring(0, 9);
String latitude = String.valueOf(location.getLatitude()).substring(0, 9);


String coordinates[] = {latitude,longitude};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);

p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));

mc.animateTo(p);
mMapView.setBuiltInZoomControls(true);

mMapView.setSatellite(false);
mc.setZoom(16);




/*Harita
public View mMapViewContainer;
public MapView mMapView;
public MapController mc;
public GeoPoint p;

public List<Overlay> mapOverlays;
public Drawable drawable;
public MapItemizedOverlay itemizedOverlay;
Harita*/

drawable = this.getResources().getDrawable(R.drawable.map_kirmizi_png);
itemizedOverlay = new MapItemizedOverlay(drawable, parent.getApplicationContext(), 30);//text size: 30

GeoPoint gPointMe = new GeoPoint(41099932,29002657);
OverlayItem overlayItem = new OverlayItem(gPointMe, "Me", "This is my location");
itemizedOverlay.addOverlay(overlayItem);

mapOverlays.add(itemizedOverlay);

mMapView.setBuiltInZoomControls(true);

//move map over to my position
mc.animateTo(gPointMe);



mMapView.invalidate();
}

}

我想,我在处理这段代码时遇到了错误:

protected OverlayItem createItem(int i)
{
return mOverlays.get(i);
}

这些是我的 LogCat。

12-21 12:35:53.443: E/AndroidRuntime(19782): FATAL EXCEPTION: main
12-21 12:35:53.443: E/AndroidRuntime(19782): java.lang.NullPointerException
12-21 12:35:53.443: E/AndroidRuntime(19782): at com.mert.fragment.BaseFragment.createMapViewForHaritaFragment(BaseFragment.java:274)
12-21 12:35:53.443: E/AndroidRuntime(19782): at com.mert.fragment.impl.EtrafimdaHaritaFragment$MyHandler.handleMessage(EtrafimdaHaritaFragment.java:154)
12-21 12:35:53.443: E/AndroidRuntime(19782): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 12:35:53.443: E/AndroidRuntime(19782): at android.os.Looper.loop(Looper.java:143)
12-21 12:35:53.443: E/AndroidRuntime(19782): at android.app.ActivityThread.main(ActivityThread.java:4277)
12-21 12:35:53.443: E/AndroidRuntime(19782): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 12:35:53.443: E/AndroidRuntime(19782): at java.lang.reflect.Method.invoke(Method.java:507)
12-21 12:35:53.443: E/AndroidRuntime(19782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-21 12:35:53.443: E/AndroidRuntime(19782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-21 12:35:53.443: E/AndroidRuntime(19782): at dalvik.system.NativeStart.main(Native Method)

感谢您的帮助。

最佳答案

        AddItemized Overlay class:

public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {

private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

private Context context;

public AddItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}

public AddItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}

@Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}

@Override
public int size() {
return mapOverlays.size();
}

@Override
protected boolean onTap(int index) {
Log.e("Tap", "Tap Performed");
return true;
}

public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}

}


Use this code in your Activity


List<Overlay> mapOverlays = mv.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.drawable);
AddItemizedOverlay itemizedOverlay =
new AddItemizedOverlay(drawable, this);
OverlayItem overlayitem = new OverlayItem(gp, "Hello", "I am here");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);


public void createMapViewForHaritaFragment(IlanList list)
{
IlanList gelenListe;
gelenListe=list;

if(gelenListe!=null)
{
MainActivity parent = getParentActivity();
mMapViewContainer = parent.getmMapViewContainer();
mMapView = parent.getmMapView();

/**/
mc = mMapView.getController();

LocationManager lm = (LocationManager) parent.getSystemService(parent.getApplicationContext().LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
String longitude = String.valueOf(location.getLongitude()).substring(0, 9);
String latitude = String.valueOf(location.getLatitude()).substring(0, 9);


String coordinates[] = {latitude,longitude};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);

p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));

mc = mMapView.getController();
mc.setCenter(p);
mc.setZoom(16);

List<Overlay> mapOverlays = mv.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.pin);
AddItemizedOverlay itemizedOverlay = new AddItemizedOverlay(drawable,
this);
OverlayItem overlayitem = new OverlayItem(p, "Hello", "I AM HERE");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);

/* mc.animateTo(p);
mMapView.setBuiltInZoomControls(true);

mMapView.setSatellite(false);
mc.setZoom(16); */




/*Harita
public View mMapViewContainer;
public MapView mMapView;
public MapController mc;
public GeoPoint p;

public List<Overlay> mapOverlays;
public Drawable drawable;
public MapItemizedOverlay itemizedOverlay;
Harita*/

/* drawable = this.getResources().getDrawable(R.drawable.map_kirmizi_png);
itemizedOverlay = new MapItemizedOverlay(drawable, parent.getApplicationContext(), 30);//text size: 30

GeoPoint gPointMe = new GeoPoint(41099932,29002657);
OverlayItem overlayItem = new OverlayItem(gPointMe, "Me", "This is my location");
itemizedOverlay.addOverlay(overlayItem);

mapOverlays.add(itemizedOverlay);

mMapView.setBuiltInZoomControls(true);

//move map over to my position
mc.animateTo(gPointMe);



mMapView.invalidate(); */
}

}

关于android - 如何在 android 上添加叠加层以映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13988744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com