gpt4 book ai didi

android - 如何在 Google Map API v2 中的 MapFragment 上显示多个标记?

转载 作者:IT老高 更新时间:2023-10-28 22:25:29 25 4
gpt4 key购买 nike

我在我的应用程序中使用 Google Map API v2 来显示 map 。

我已完成所有步骤,即在我的应用程序中启用 Google map 。

public class PinLocationOnMapView extends FragmentActivity {

private double mLatitude = 0.0, mLongitude = 0.0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

SupportMapFragment fragment = SupportMapFragment.newInstance();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, fragment).commit();
}
}

如果我使用此代码,它会向我显示 map ,但如果我提供纬度/经度值,则不会加载 map 图 block ,并且我只会看到白色图 block 。

以下是上面类的onCreate()中编写的代码:

 if (getIntent().getExtras() != null) {
final Bundle bundle = getIntent().getBundleExtra("LOCATION");
mLatitude = bundle.getDouble("LATITUDE");
mLongitude = bundle.getDouble("LONGITUDE");
} else {
finish();
}

GoogleMapOptions options = new GoogleMapOptions();
LatLng latLng = new LatLng(mLatitude, mLongitude);
CameraPosition cameraPosition;// = new CameraPosition(latLng, 0, 0, 0);
cameraPosition = CameraPosition.fromLatLngZoom(latLng, (float) 14.0);

options.mapType(GoogleMap.MAP_TYPE_SATELLITE).camera(cameraPosition)
.zoomControlsEnabled(true).zoomGesturesEnabled(true);

SupportMapFragment fragment = SupportMapFragment.newInstance(options);
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, fragment).commit();

另外,我有一个纬度/经度值列表。我想在 MapFragment 上显示它们,如何在 MapFragment 上显示多个标记?

我尝试使用 MapViewItemizedOverlay,但它对我不起作用。我相信我已经正确创建了 SHA1 key 来获取 API key ,因为如果那是错误的,我无法使用 MapFragment 看到 map 好吧,但我可以看到,如果我不传递 lat/log 值。

最佳答案

我这样做是为了在 map 上用不同颜色的标记显示汽车位置:

    private void addMarkersToMap() {
mMap.clear();
for (int i = 0; i < Cars.size(); i++) {
LatLng ll = new LatLng(Cars.get(i).getPos().getLat(), Cars.get(i).getPos().getLon());
BitmapDescriptor bitmapMarker;
switch (Cars.get(i).getState()) {
case 0:
bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED);
Log.i(TAG, "RED");
break;
case 1:
bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN);
Log.i(TAG, "GREEN");
break;
case 2:
bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE);
Log.i(TAG, "ORANGE");
break;
default:
bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED);
Log.i(TAG, "DEFAULT");
break;
}
mMarkers.add(mMap.addMarker(new MarkerOptions().position(ll).title(Cars.get(i).getName())
.snippet(getStateString(Cars.get(i).getState())).icon(bitmapMarker)));

Log.i(TAG,"Car number "+i+" was added " +mMarkers.get(mMarkers.size()-1).getId());
}
}

}

Cars 是自定义对象的 ArrayList,mMarkers 是标记的 ArrayList

注意:您可以像这样在 fragment 中显示 map :

private GoogleMap mMap;
....

private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}




private void setUpMap() {
// Hide the zoom controls as the button panel will cover it.
mMap.getUiSettings().setZoomControlsEnabled(false);

// Add lots of markers to the map.
addMarkersToMap();

// Setting an info window adapter allows us to change the both the
// contents and look of the
// info window.
mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());

// Set listeners for marker events. See the bottom of this class for
// their behavior.
mMap.setOnMarkerClickListener(this);
mMap.setOnInfoWindowClickListener(this);
mMap.setOnMarkerDragListener(this);

// Pan to see all markers in view.
// Cannot zoom to bounds until the map has a size.
final View mapView = getSupportFragmentManager().findFragmentById(R.id.map).getView();
if (mapView.getViewTreeObserver().isAlive()) {
mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressLint("NewApi")
// We check which build version we are using.
@Override
public void onGlobalLayout() {
LatLngBounds.Builder bld = new LatLngBounds.Builder();
for (int i = 0; i < mAvailableCars.size(); i++) {
LatLng ll = new LatLng(Cars.get(i).getPos().getLat(), Cars.get(i).getPos().getLon());
bld.include(ll);
}
LatLngBounds bounds = bld.build();
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 70));
mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

}
});
}
}

只需在 onCreate()

中调用 setUpMapIfNeeded()

关于android - 如何在 Google Map API v2 中的 MapFragment 上显示多个标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13855049/

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