I'm currently developing an app for Android, using Google Maps API v2. I'm really new to android developing, even if I studied (and I'm still studying) Java at university.
I managed to create the map, and to add my personal markers to it. Each marker need its own Info Window, and this is the problem. I can only modify Title and Snippet, but in the box I need to put also different pictures for different marker.
Markers represent Libraries.
Example: Marker 1: name services (different images for air conditioning, food, plugs and so on) number of seats available
Name is not a problem: it's title. Seats as well is represented by the snippet. But the images are the problem: I can't find a way to show different images in different windows.
Here's the Java code for the Main Activity:
package com.matteocunsolo.apposto;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
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;
}
}
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initializeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initializeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.getUiSettings().setZoomGesturesEnabled(true);
if(googleMap != null) {
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
}
googleMap.getUiSettings().setRotateGesturesEnabled(true);
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(41.903021, 12.514744)).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
createMarkers();
googleMap.setInfoWindowAdapter(new MyInfoWindowAdapter());
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
void createMarkers() {
String name = "Biblioteca Alessandrina";
double latitude = 41.903021;
double longitude = 12.514744;
Marker m = googleMap.addMarker(new MarkerOptions().title(name).position(new LatLng(latitude, longitude)).snippet("250"));
String name2 = "Biblioteca CIAO";
double latitude2 = 41.906332;
double longitude2 = 12.517669;
Marker m2 = googleMap.addMarker(new MarkerOptions().title(name2 + "\n").position(new LatLng(latitude2, longitude2)).snippet("134"));
}
@Override
protected void onResume() {
super.onResume();
initializeMap();
}
}
And here's the XML for the Info Window:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:text="title"
android:textSize="25dp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="23dp"
android:layout_marginLeft="5dp"
android:text="Services: "
android:textSize="20dp" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Seats: "
android:textSize="20dp" />
<TextView
android:id="@+id/snippet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="20dp"
android:text="150" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
I can only modify Title and Snippet, but in the box I need to put also different pictures for different marker.
Your getInfoContents()
method can certainly change more than that.
But the images are the problem: I can't find a way to show different images in different windows
Retrieve the ImageView
using findViewById()
, and set the image you want to use for it, in your getInfoContents()
method. This is no different than how you are changing the TextView
widgets.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.