自定义信息窗在导航抽屉中有多个标记

时间:2015-06-11 08:12:12

标签: android google-maps

毕竟,我一直在搜索这几个小时,没有什么可以帮助我...

我想在Google Map API v2 for Android中为多个标记添加自定义信息窗口。

我在导航抽屉里有这个,我尝试了很多没有结果的东西。

这是我的代码:

public class ShopLocator extends Fragment {
MapView mapView;
GoogleMap googleMap;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.shop_locator, container, false);

    mapView = (MapView) view.findViewById(R.id.map);

    mapView.onCreate(savedInstanceState);

    if(mapView!=null) {

        Toast.makeText(getActivity(), "For a better performance of the shop locator, we recommend to activate the GPS", Toast.LENGTH_LONG).show();

        googleMap = mapView.getMap();

        googleMap.getUiSettings().setMyLocationButtonEnabled(true);

        googleMap.setMyLocationEnabled(true);

        googleMap.getUiSettings().setZoomControlsEnabled(true);

        final Location[] mLocation = {null};
        googleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
            public void onMyLocationChange(Location location) {

                CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude()));
                CameraUpdate zoom = CameraUpdateFactory.zoomTo(8);

                if (mLocation[0] == null) {
                    mLocation[0] = location;
                    googleMap.moveCamera(center);
                    googleMap.animateCamera(zoom);
                }

            }
        });

    }

    return view;
}
}

1 个答案:

答案 0 :(得分:0)

看一下这段代码:

public class MainActivity extends Activity 
implements OnMapLongClickListener{

class MyInfoWindowAdapter implements InfoWindowAdapter{

    private final View myContentsView;

    MyInfoWindowAdapter(){
        myContentsView = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
    }

    @Override
    public View getInfoContents(Marker marker) {

        TextView tvTitle = ((TextView)myContentsView.findViewById(R.id.title));
        tvTitle.setText(marker.getTitle());
        TextView tvSnippet = ((TextView)myContentsView.findViewById(R.id.snippet));
        tvSnippet.setText(marker.getSnippet());

        return myContentsView;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        // TODO Auto-generated method stub
        return null;
    }

}

final int RQS_GooglePlayServices = 1;
GoogleMap myMap;
TextView tvLocInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tvLocInfo = (TextView)findViewById(R.id.locinfo);

    FragmentManager myFragmentManager = getFragmentManager();
    MapFragment myMapFragment 
        = (MapFragment)myFragmentManager.findFragmentById(R.id.map);
    myMap = myMapFragment.getMap();

    myMap.setMyLocationEnabled(true);

    //myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    //myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    //myMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    myMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

    myMap.getUiSettings().setZoomControlsEnabled(true);
    myMap.getUiSettings().setCompassEnabled(true);
    myMap.getUiSettings().setMyLocationButtonEnabled(true);

    myMap.getUiSettings().setRotateGesturesEnabled(true);
    myMap.getUiSettings().setScrollGesturesEnabled(true);
    myMap.getUiSettings().setTiltGesturesEnabled(true);
    myMap.getUiSettings().setZoomGesturesEnabled(true);
    //or myMap.getUiSettings().setAllGesturesEnabled(true);

    myMap.setTrafficEnabled(true);

    myMap.setOnMapLongClickListener(this);

    myMap.setInfoWindowAdapter(new MyInfoWindowAdapter());

}

@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_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_legalnotices:
        String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
                getApplicationContext());
        AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
        LicenseDialog.setTitle("Legal Notices");
        LicenseDialog.setMessage(LicenseInfo);
        LicenseDialog.show();
        return true;    
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {

    super.onResume();

    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

    if (resultCode == ConnectionResult.SUCCESS){
        Toast.makeText(getApplicationContext(), 
                "isGooglePlayServicesAvailable SUCCESS", 
                Toast.LENGTH_LONG).show();  
    }else{
        GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);    
    }
}

@Override
public void onMapLongClick(LatLng point) {
    tvLocInfo.setText("New marker added@" + point.toString());

    Marker newMarker = myMap.addMarker(new MarkerOptions()
                            .position(point)
                            .snippet(point.toString()));

    newMarker.setTitle(newMarker.getId());

}

}

正如您所看到的,用于创建自定义信息窗口的代码位于MyInfoWindowAdapter()方法中,但在此之前,您应该有TITLE和SNIPPET,它位于上面示例中的onMapLongClick()方法上。

您还必须创建一个自定义XML布局,用于制作自定义信息窗口。此自定义XML布局是" layout.custom_info_contents"从上面的代码。

希望它有所帮助:D