gpt4 book ai didi

android - 无法在 Google map 上添加多个标记

转载 作者:太空狗 更新时间:2023-10-29 13:32:18 24 4
gpt4 key购买 nike

在我的应用程序中,我使用的是 Google map ,首先我通过标记向用户显示当前位置,并通过透明圆圈显示其准确性,现在当我想将另一个标记添加到代表我之前位置的同一点时,使用相同的代码对我不起作用,请有人帮我弄清楚代码有什么问题,我只想在单个 map View 中添加多个标记

这是我显示两个标记的代码:-

LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(Location location){
//Got the location!
if (location != null) {
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));

accuracy = location.getAccuracy();
cur_lati = location.getLatitude();
cur_longi = location.getLongitude();

String address = "";
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);

if (addresses.size() > 0) {
for (int index = 0;
index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
}
}catch (IOException e) {
e.printStackTrace();
}


Input.setPinPoints(cur_lati,cur_longi,address); // sending lat,lon to input class

Toast.makeText(getBaseContext(),"Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude() + "accuracy" + location.getAccuracy(),Toast.LENGTH_SHORT).show();
mc = mapView.getController();
mc.animateTo(point);
mc.setZoom(19);


MapOverlay mapOverlay = new MapOverlay();
mapOverlay.setPointToDraw(point);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);

Bundle bundle=getIntent().getExtras();

if(bundle!=null){

int value = bundle.getInt("tab_value");
lati = bundle.getDouble("lati");
longi = bundle.getDouble("longi");



//GeoPoint point1 = new GeoPoint((int) lati, (int) longi);

if(value == 1){

MapOverlay1 mapOverlay1 = new MapOverlay1();
// mapOverlay1.setPointToDraw(point1);
List<Overlay> listOfOverlays1 = mapView.getOverlays();
listOfOverlays1.add(mapOverlay1);


}

}

}
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);


btnimg.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

Intent intent=new Intent(getApplicationContext(),Input.class);
startActivity(intent);

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_safe_city, menu);
return true;
}

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

@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();

finish();
}

@Override
public void finish() {
// TODO Auto-generated method stub
super.finish();

}


class MapOverlay extends Overlay
{
private GeoPoint pointToDraw;

public void setPointToDraw(GeoPoint point) {
pointToDraw = point;
}

public GeoPoint getPointToDraw() {
return pointToDraw;
}

@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);

// convert point to pixels
Point screenPts = new Point();
Paint mPaintBorder,mPaintFill ;
mapView.getProjection().toPixels(pointToDraw, screenPts);

// add marker
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pinimage);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null);

mPaintBorder = new Paint();
mPaintBorder.setStyle(Paint.Style.STROKE);
mPaintBorder.setAntiAlias(true);
mPaintBorder.setColor(0xee4D2EFF);
mPaintFill = new Paint();
mPaintFill.setStyle(Paint.Style.FILL);
mPaintFill.setColor(0x154D2EFF);

int radius = (int) mapView.getProjection().metersToEquatorPixels(accuracy);

canvas.drawCircle(screenPts.x, screenPts.y, radius, mPaintBorder);

canvas.drawCircle(screenPts.x, screenPts.y, radius, mPaintFill);

return true;
}
}

class MapOverlay1 extends com.google.android.maps.Overlay
{

private GeoPoint p1 = new GeoPoint((int)(lati),(int)(longi));

Projection projection;



@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{

super.draw(canvas, mapView, shadow);


Point screenPts1 = new Point();
mapView.getProjection().toPixels(p1, screenPts1);
//---add the marker---
Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.car);
canvas.drawBitmap(bmp1, screenPts1.x, screenPts1.y-50, null);

return true;

}


}

通过使用 MapOverlay 类,我可以在显示用户当前位置的 map 上放置一个图钉,但是当我使用 MapOverlay1 显示第二个标记时,它不起作用....

任何帮助将不胜感激提前致谢......

最佳答案

切换到谷歌地图的新 API。使用起来好多了。如果您必须使用旧 API,请为您的当前位置使用 MyLocationOverlay。另外,如果我没记错的话,有一对 set/getMarker 方法可以省去您自己在 Canvas 上绘制标记的麻烦。

关于android - 无法在 Google map 上添加多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14236152/

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