如何显示在MapFragment在谷歌地图API V2多个标记? [英] How to show multiple markers on MapFragment in Google Map API v2?

查看:168
本文介绍了如何显示在MapFragment在谷歌地图API V2多个标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用谷歌地图API V2 显示地图。

I am using Google Map API v2 in my application to show Maps.

我按照所有的步骤,那就是要遵循,使谷歌地图在我的应用程序。

I have followed all the steps, that is to be followed to enable Google Map in my application.

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

如果我用这个code,它表明我的地图,但如果我提供我的纬度/经度值,地图图块不加载,而我只看到白色的瓷砖。

If I use this code, it shows me map, but if I provide my latitude/longitude values, map tiles does not load, and I see only white tiles.

以下是code写的onCreate()上面的类的:

Following is the code written in onCreate() of above class:

 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 多个标记

Also, I have a list of lat/long values. I want to show them on MapFragment, how to show multiple markers on MapFragment?

我试着用图形页面 ItemizedOverlay ,但它没有为我工作。我相信我已经正确地创建了 SHA1 键进入 API 键,因为如果错了,我不能看地图,使用 MapFragment 为好,但我可以看到,如果我没有通过纬度/经度值。

I tried with MapView and ItemizedOverlay, but it didn't work for me. I believe I have correctly created the SHA1 key to get the API key, because if that was wrong, I could not see map using MapFragment as well, but I can see that if I don't pass the lat/log value.

推荐答案

我不喜欢这样,以展车的位置在地图上用不同颜色的标记:

I do it like this to show car positions on the map with markers of different colors:

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

}

汽车是一个自定义对象和mMarkers的的ArrayList ArrayList的标记

请注意:您可以显示地图中的片段是这样的:

Note : You can show map in fragment like this:

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

            }
        });
    }
} 

和只需拨打 setUpMapIfNeeded()的onCreate()

这篇关于如何显示在MapFragment在谷歌地图API V2多个标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆