如何正确设计信息风格?

时间:2016-05-19 16:00:29

标签: java android android-studio infowindow

我试图在地图上显示用户位置,但是必须设置信息风格。我尝试使用setInfoWindowAdapter来设置infowindow的样式,但它并没有应用样式。

这是我的代码:

public void handleNewLocation(Location location){
    Log.d(TAG, location.toString());
    mMap.clear();

    double currentLatitude = location.getLatitude();
    double currentLongitude = location.getLongitude();
    LatLng latLng = new LatLng(currentLatitude, currentLongitude);

    List<android.location.Address> addresses = null;
    geocoder = new Geocoder(this, Locale.getDefault());

    try {
        addresses = geocoder.getFromLocation(currentLatitude, currentLongitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
    } catch (IOException e) {
        e.printStackTrace();
    }

    String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
    String city = addresses.get(0).getLocality();
    String country = addresses.get(0).getCountryName();
    String postalCode = addresses.get(0).getPostalCode();

    //mMap.addMarker(new MarkerOptions().position(new LatLng(currentLatitude, currentLongitude)).title("Current Location"));
    MarkerOptions options = new MarkerOptions()
            .position(latLng)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker))
            .title("Uw locatie:")
            .snippet(address+", "+postalCode+" "+city+", "+country);
    Marker locationMarker = mMap.addMarker(options);
    pointToPosition(latLng);
    locationMarker.showInfoWindow();

    mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }

        @Override
        public View getInfoContents(Marker marker) {

            Context context = getApplicationContext(); //or getActivity(), YourActivity.this, etc.

            LinearLayout info = new LinearLayout(context);
            info.setOrientation(LinearLayout.VERTICAL);

            TextView title = new TextView(context);
            title.setTextColor(Color.BLACK);
            title.setGravity(Gravity.CENTER);
            title.setTypeface(null, Typeface.BOLD);
            title.setText(marker.getTitle());

            TextView snippet = new TextView(context);
            snippet.setTextColor(Color.GRAY);
            snippet.setText(marker.getSnippet());

            info.addView(title);
            info.addView(snippet);

            return info;
        }
    });
}

0 个答案:

没有答案