gpt4 book ai didi

android - 使用来自对象类的经纬度在 Google map 上绘制 Pins/MapOverlays

转载 作者:搜寻专家 更新时间:2023-11-01 09:08:18 24 4
gpt4 key购买 nike

我在下面有这个 MapActivity 类,用于显示 map 并放置用户当前位置的图钉。另一方面,我有一个对象类 VideoLocation,它具有从服务器的 JSON 文件中提取的一些地方的纬度和经度。我还希望在 map 上用图钉绘制位置。

我怎样才能做到这一点?我需要在下面的类(class)中添加什么?

仅供引用,这是我在 ListActivity 上调用对象的纬度和经度的方式:

VideoLocation vidLocation = videoLocations[position];

double lat2 = vidLocation.latitude;
double lng2 = vidLocation.longitude;

这个类:

public class MyMapActivity extends MapActivity implements OnClickListener {
public static final String TAG = "GoogleMapsActivity";
private MapView mapView;
private LocationManager locationManager;
Geocoder geocoder;
Location location;
LocationListener locationListener;
CountDownTimer locationtimer;
MapController mapController;
MapOverlay mapOverlay = new MapOverlay();

private VideoLocation[] videoLocations = null;

@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.my_map_activity);
initComponents();

JsonDB dbhelper = new JsonDB(MyMapActivity.this);
SQLiteDatabase db = dbhelper.getWritableDatabase();

db.beginTransaction();
videoLocations = dbhelper.getVideoLocations(db);
db.setTransactionSuccessful();//end transaction
db.endTransaction();
db.close();

final ImageButton refresh = (ImageButton) findViewById(R.id.btn_nav_impressum);
refresh.setOnClickListener(this);

final ImageButton search =(ImageButton) findViewById(R.id.btn_nav_locater);
search.setOnClickListener(this);



locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (locationManager == null) {
Toast.makeText(MyMapActivity.this,
"Location Manager Not Available", Toast.LENGTH_SHORT)
.show();
return;
}
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {

animateMap(location);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
}
locationListener = new LocationListener() {
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}

@Override
public void onProviderEnabled(String arg0) {
}

@Override
public void onProviderDisabled(String arg0) {
}

@Override
public void onLocationChanged(Location l) {
location = l;
locationManager.removeUpdates(this);
if (l.getLatitude() == 0 || l.getLongitude() == 0) {
} else {
double lat = l.getLatitude();
double lng = l.getLongitude();
showAddress(lat,lng);
}
}
};
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 1000, 10f, locationListener);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener);
locationtimer = new CountDownTimer(30000, 5000) {
@Override
public void onTick(long millisUntilFinished) {
if (location != null)
locationtimer.cancel();
}

@Override
public void onFinish() {
if (location == null) {
}
}
};
locationtimer.start();
}

public void animateMap(Location location){
double lat = location.getLatitude();
double lng = location.getLongitude();

GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
GeoPoint point2 = new GeoPoint((int) (lat2 * 1E6), (int) (lng2 * 1E6));
mapController.animateTo(point, new Message());
mapOverlay.setPointToDraw(point);

}


public MapView getMapView() {
return this.mapView;
}

private void initComponents() {
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(16);
}

@Override
protected boolean isRouteDisplayed() {
return false;
}

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);

Point screenPts = new Point();
mapView.getProjection().toPixels(pointToDraw, screenPts);

Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.pin_normal);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null);
return true;
}

最佳答案

如果您想在 map 顶部添加图钉(或图像),添加 ItemizedOverlay 类会更好。
包 com.android.testandroidmap;

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 {

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;

public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
mContext = context;
}

@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}

@Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}

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

@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}

我从 Android Developer Official Guide 得到这个代码,并在您的代码中使用此类,您可以添加叠加层。

添加您需要的所有东西(容器、图钉图像和图钉+坐标叠加层)

List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);

为了你的图标被之前的类识别,同时实例化坐标,将坐标添加到一个overlayItem + 将overlayItem注册到你之前制作的HelloItemizedOverlay

GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);

最后将叠加层添加到 mapOverlay,它是叠加层的集合。

mapOverlays.add(itemizedoverlay);

这会在我的 map 顶部显示一个图钉,可以根据您想要的任何图像对其进行自定义。如果您还想为每个位置使用不同的图像,只需用您的图像实例化另一个 Drawable 并将该 Drawable 注册到其他 HelloItemizedOverlay。

关于android - 使用来自对象类的经纬度在 Google map 上绘制 Pins/MapOverlays,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10248648/

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