Google地图v2的自定义信息窗口

时间:2014-09-19 02:55:47

标签: android google-maps google-maps-markers google-maps-android-api-2

我正在尝试自定义信息窗口并在加载地图时显示它。自定义信息窗口出现,但是,与默认的infowindow背景(白色)一起,我还没有为它出现的标记设置图标?为什么我会得到这个默认值?

以下是我尝试的屏幕截图:

enter image description here

我不想要白色窗口和标记图标?我正在尝试实现类似于以下内容的截图:How to create a custom-shaped bitmap marker with Android map API v2

以下是我的尝试:

public class Mapstwo extends Fragment implements GoogleMap.OnMyLocationChangeListener
{
private GoogleMap googleMap;
private GPSTracker gps;
private double curlat;
private double curlong;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) 
{
    if (rootView != null) 
    {
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null)
            parent.removeView(rootView);
    }
    try 
    {
        rootView = inflater.inflate(R.layout.activity_locate_me_maps, container, false);
    } 
    catch (InflateException e) 
    {

    }

            if (initMap())
    {
        gps = new GPSTracker(fa);
        curlat = gps.getLatitude();
        curlong = gps.getLongitude();
        gps.stopUsingGPS();

        myLocation(curlat, curlong, username, imageURL, ZOOMVALUE);
    }
    else
    {
        Toast.makeText(getActivity().getApplicationContext(),"Sorry! Map not available!", Toast.LENGTH_SHORT).show();
    }


    return rootView;
}


    private boolean initMap() 
{
    if (googleMap == null)
    {
        SupportMapFragment mapFrag = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map));
        googleMap = mapFrag.getMap();
        googleMap.setTrafficEnabled(true);
        //googleMap.setMyLocationEnabled(true);
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        googleMap.setOnMyLocationChangeListener(this);
    }

    return (googleMap != null);
}


    //MY LOCATION
private void myLocation(double lat, double lng, String name, String url, float zoom)
{

    final String uname = name; 
    curlat = getLat; 
    curlong = getLong;
    LatLng position = new LatLng(curlat, curlong);

    markermylocation = googleMap.addMarker(new MarkerOptions().position(position).title(uname).snippet("Testing is everthing OK?")); 
    markers.put(markermylocation.getId(), imageURL);
    googleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter()); 
    markermylocation.showInfoWindow();

}


    @SuppressLint("InflateParams") 
private class CustomInfoWindowAdapter implements InfoWindowAdapter 
{
    @Override
    public View getInfoWindow(Marker arg0) 
    {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) 
    {
        View v = getActivity().getLayoutInflater().inflate(R.layout.activity_map, null);  

        Log.e("View","Sucessful");

        return v;
    }
}

}

1 个答案:

答案 0 :(得分:1)

尝试此代码,您需要更改默认布局框架,您需要在getInfoWindow()

中设置您的膨胀视图
// Use default InfoWindow frame
    @Override
    public View getInfoWindow(Marker arg0) {
        View v = getActivity().getLayoutInflater().inflate(R.layout.activity_map, null);                                                                              
        Log.e("View","Sucessful");
        return v;
    }

   // Defines the contents of the InfoWindow
    @Override
    public View getInfoContents(Marker arg0) {
       return null;
     }

您在默认内容布局中夸大了视图,因此请更改以使视图膨胀。