Display image in infowindow from URL Google Maps Android API - android

I have a map in a fragment in a navigation drawer. Now the code for the map is found in the MainActivity.java of the navigation drawer. I have a for loop inside onMapReady which pin markers on my map. Now each iteration of the for loop takes retrieved data from Firebase, to be able to pin the markers. The retrieved data also contains URLs for images and I need to use those URLs to display an image in the infowindow of each marker. I've tried to understand the other solutions provided but I haven't got any idea how to implement this.
this is my code so far my code:
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
...
for (infoToStore details : info) {
marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.parseDouble(details.getLat()), Double.parseDouble(details.getLng())))
.title(details.getName())
.snippet(details.getDesc()));
}
}
EDIT
I've tried to implement it as follows but the infowindow is blank; not showing the TextViews nor the ImageView.
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
for (final infoToStore details : info) {
marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.parseDouble(details.getLat()), Double.parseDouble(details.getLng())))
.title(details.getName())
.snippet(details.getDesc()));
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
View v = getLayoutInflater().inflate(R.layout.popup, null);
TextView name = (TextView) v.findViewById(R.id.name);
TextView desc = (TextView) v.findViewById(R.id.desc);
ImageView image = (ImageView) v.findViewById(R.id.image);
name.setText(marker.getTitle());
desc.setText(marker.getSnippet());
Picasso.with(getApplicationContext())
.load(URLString)
.into(image);
return v;
}
});
}
}
here's the popup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/image"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/desc"/>
</LinearLayout>

Try this:
Create a Global Marker Variable
private Marker marker;
Now in your onMapReady() call
mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
Create your CustomInfoWindowAdapter class and add the following code..
private class CustomInfoWindowAdapter implements InfoWindowAdapter {
private View view;
public CustomInfoWindowAdapter() {
view = getLayoutInflater().inflate(R.layout.popup,null);
}
#Override
public View getInfoContents(Marker marker) {
if (MainActivity.this.marker != null
&& MainActivity.this.marker.isInfoWindowShown()) {
MainActivity.this.marker.hideInfoWindow();
MainActivity.this.marker.showInfoWindow();
}
return null;
}
#Override
public View getInfoWindow(final Marker marker) {
MainActivity.this.marker = marker;
TextView name = (TextView) view.findViewById(R.id.name);
TextView desc = (TextView) view.findViewById(R.id.desc);
ImageView image = (ImageView) view.findViewById(R.id.image);
Picasso.with(getApplicationContext())
.load(URLString)
.error(R.mipmap.ic_launcher) // will be displayed if the image cannot be loaded
.into(image);
final String title = marker.getTitle();
if (title != null) {
name.setText(title);
} else {
name.setText("Default");
}
final String snippet = marker.getSnippet();
if (snippet != null) {
desc.setText(snippet);
} else {
desc.setText("Deafult");
}
//getInfoContents(marker);
return view;
}
}
your imageview is very large...which blocks the textView: try this layout in your popup
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Sample Text"
android:textColor="#000000"
android:textSize="15sp" />
<TextView
android:id="#+id/desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Sample Text2"
android:textColor="#000000"
android:textSize="12sp" />
</LinearLayout>
And for different image you need a arraylist of imageURL for different markers in the map.

Related

Unable to display MapView in LiteMode inside RecyclerView

I have a list of saved locations coming from the server that I want to display inside a RecyclerView with a map snapshot of the location.
I coded it according to code samples provided for the same by Google but unfortunately the list wouldn't show on the UI.
I am attaching the code to my RecyclerView Adapter & item XML.
public class SavedLocationAdapter extends RecyclerView.Adapter<SavedLocationAdapter.ViewHolder> {
private Context mContext;
private ArrayList<SavedLocationDetails> savedLocationList;
private LoadMoreLocationsInterface loadMoreLocationsInterface;
public SavedLocationAdapter(Context context, LoadMoreLocationsInterface loadMoreLocationsInterface) {
//super(DIFF_CALLBACK);
super();
mContext = context;
this.savedLocationList = new ArrayList<>();
this.loadMoreLocationsInterface = loadMoreLocationsInterface;
}
public void updateList(List<SavedLocationDetails> savedLocationList) {
this.savedLocationList.addAll(savedLocationList);
notifyDataSetChanged();
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_saved_location, parent, false));
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
SavedLocationDetails savedLocationDetails = savedLocationList.get(position);
if (savedLocationDetails != null)
holder.bindView(savedLocationDetails);
if (position == getItemCount()-1)
loadMoreLocationsInterface.loadMoreLocations(savedLocationDetails.getLid());
}
#Override
public int getItemCount() {
return savedLocationList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {
MapView mapView;
TextView locationType;
TextView friendlyName;
TextView fullAddress;
public GoogleMap map;
View layout;
public ViewHolder(#NonNull View itemView) {
super(itemView);
layout = itemView;
locationType = layout.findViewById(R.id.savedLocationTypeTV);
fullAddress = layout.findViewById(R.id.savedLocationAddressTV);
friendlyName = layout.findViewById(R.id.friendlyNameTV);
mapView = layout.findViewById(R.id.savedLocationMap);
if(mapView != null) {
mapView.onCreate(null);
mapView.onResume();
mapView.onPause();
mapView.getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(mContext);
map = googleMap;
setMapLocation();
}
private void setMapLocation() {
if (map == null)
return;
SavedLocationDetails savedLocationDetails = (SavedLocationDetails) mapView.getTag();
if (savedLocationDetails == null)
return;
LatLng locationLatLng = new LatLng(savedLocationDetails.getLat(), savedLocationDetails.getLng());
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.location_marker_black_solid);
MarkerOptions markerOptions = (new MarkerOptions()).position(locationLatLng).icon(icon);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(locationLatLng, 18f));
map.addMarker(markerOptions);
}
public void bindView(SavedLocationDetails savedLocationDetails) {
layout.setTag(this);
mapView.setTag(savedLocationDetails);
setMapLocation();
if((savedLocationDetails.getLtp() != null) && (savedLocationDetails.getLtp() == 3)) {
locationType.setVisibility(GONE);
friendlyName.setText(savedLocationDetails.getFn());
friendlyName.setVisibility(View.VISIBLE);
} else {
friendlyName.setVisibility(GONE);
locationType.setVisibility(View.VISIBLE);
locationType.setText("Home");
}
fullAddress.setText(savedLocationDetails.getAdd());
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardBackgroundColor="#color/appBar"
app:cardElevation="3dp"
app:cardCornerRadius="30dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.gms.maps.MapView
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/savedLocationMap"
android:layout_width="match_parent"
android:layout_height="225dp"
app:mapType="normal"
map:liteMode="true"
map:mapType = "normal" />
<RelativeLayout
android:id="#+id/savedLocationDataRL"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignBottom="#+id/savedLocationMap"
android:background="#color/white"
android:visibility="gone">
<TextView
android:id="#+id/savedLocationTypeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="Work"
android:textColor="#color/quantum_black_100"
android:textStyle="bold"
android:textSize="15sp"/>
<TextView
android:id="#+id/friendlyNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="Friendly Name"
android:textColor="#color/quantum_black_100"
android:textSize="15sp"
android:textStyle="bold"
android:visibility="gone"/>
<TextView
android:id="#+id/savedLocationAddressTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/savedLocationTypeTV"
android:layout_marginTop="3dp"
android:layout_marginStart="10dp"
android:text="Full Address"
android:textColor="#color/quantum_black_100"
android:textSize="13sp"/>
</RelativeLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>

MapView inside RecyclerView only displaying map on clicks

This has to be the most absurd thing ever.
I have a RecyclerView with repeated custom items. Inside these items, there are a few textfields, buttons and a single MapView.
The issue is that when the list loads, the MapView only displays the Google logo and no other tile or detail (or marker). However, when I tap once on the map, it shows the marker I added. On the next tap, it loads a pixellated map. On another tap, it loads a better quality map. On further clicks it adds the text labels for nearby locations. LatLngBounds are also not working but that's a secondary problem.
Why is this happening?
My code is as follows:
JobAdapter.java
public class JobAdapter extends RecyclerView.Adapter<JobAdapter.ViewHolder>
{
private Context context;
private static List<Job> jobList;
private HashSet<MapView> mapViews = new HashSet<>();
private GoogleMap googleMap;
public JobAdapter(Context con, List<Job> jobs)
{
context = con;
jobList = jobs;
}
#Override
public int getItemCount()
{
return jobList.size();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.listitem_booking, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
mapViews.add(viewHolder.mapView);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position)
{
Job job = jobList.get(position);
holder.mapView.setClickable(false);
if(job.getJobType().equalsIgnoreCase("Now"))
{
holder.pickup.setBackgroundColor(ContextCompat.getColor(context, R.color.lightRed));
}
holder.pickup.setText(job.getPickupAddress());
if(job.getDestinationAddress() != null && !job.getDestinationAddress().equalsIgnoreCase(""))
{
holder.destination.setText(job.getDestinationAddress());
}
else
{
holder.destination.setVisibility(View.GONE);
}
holder.person.setText(job.getContact());
holder.datetime.setText(job.getDate() + " at " + job.getTime());
}
class ViewHolder extends RecyclerView.ViewHolder implements /*View.OnClickListener,*/ OnMapReadyCallback
{
#BindView(R.id.pickup)
TextView pickup;
#BindView(R.id.destination)
TextView destination;
#BindView(R.id.person)
TextView person;
#BindView(R.id.datetime)
TextView datetime;
#BindView(R.id.map_listitem)
MapView mapView;
#BindView(R.id.acceptJob)
Button acceptJob;
#BindView(R.id.declineJob)
Button declineJob;
#BindView(R.id.buttonLayout)
LinearLayout buttonLayout;
private ViewHolder(View itemView)
{
super(itemView);
ButterKnife.bind(this, itemView);
// itemView.setOnClickListener(this);
mapView.onCreate(null);
mapView.getMapAsync(this);
}
private void addMarkers(Job job)
{
googleMap.clear();
boolean hasDestination = true;
String[] destinationLatlng = null;
LatLng destination = null;
if(job.getDestinationAddress() == null || job.getDestinationAddress().equalsIgnoreCase(""))
{
hasDestination = false;
}
else
{
destinationLatlng = job.getDestinationLatLong().split(",");
destination = new LatLng(Double.valueOf(destinationLatlng[0]), Double.parseDouble(destinationLatlng[1]));
}
final String[] pickupLatlng = job.getPickupLatLong().split(",");
final LatLng pickup = new LatLng(Double.valueOf(pickupLatlng[0]), Double.parseDouble(pickupLatlng[1]));
if(hasDestination)
{
googleMap.addMarker(new MarkerOptions()
.position(pickup)
.title(job.getPickupAddress()));
googleMap.addMarker(new MarkerOptions()
.position(destination)
.title(job.getDestinationAddress()));
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(pickup);
builder.include(destination);
LatLngBounds bounds = builder.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 5);
googleMap.animateCamera(cameraUpdate);
}
else
{
googleMap.addMarker(new MarkerOptions()
.position(pickup)
.title(job.getPickupAddress()));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(pickup, 15);
googleMap.animateCamera(cameraUpdate);
}
}
/*#Override
public void onClick(View view)
{
final Job job = jobList.get(getAdapterPosition());
}*/
#Override
public void onMapReady(GoogleMap gMap)
{
googleMap = gMap;
addMarkers(jobList.get(getAdapterPosition()));
}
}
}
listitem_booking
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/tools"
android:orientation="vertical"
app:cardElevation="2dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/pickup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:textSize="16sp"
android:text="Complex"
android:background="#color/lessLightGreen"
android:gravity="center_vertical"
android:drawableStart="#drawable/google_maps"
android:drawablePadding="2dp"
android:textColor="#color/colorPrimaryText"/>
<TextView
android:id="#+id/destination"
android:layout_below="#id/pickup"
android:text="Golra"
android:visibility="visible"
android:drawableStart="#drawable/directions"
android:drawablePadding="2dp"
style="#style/listitem_secondary_text"/>
<TextView
android:id="#+id/person"
android:drawablePadding="2dp"
android:layout_below="#id/destination"
android:text="Asfandyar Khan"
android:drawableStart="#drawable/account"
style="#style/listitem_secondary_text"/>
<TextView
android:id="#+id/datetime"
android:layout_below="#id/person"
android:text="7th April 2017 at 9:00am"
android:drawableStart="#drawable/time"
style="#style/listitem_secondary_text"/>
<com.google.android.gms.maps.MapView
android:id="#+id/map_listitem"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_marginTop="2dp"
android:layout_below="#id/datetime"
map:liteMode="true"
android:padding="10dp"/>
<LinearLayout
android:id="#+id/buttonLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/map_listitem"
android:gravity="end"
android:layout_margin="4dp">
<Button
android:backgroundTint="#color/colorPrimary"
android:textColor="#android:color/white"
android:id="#+id/acceptJob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Accept"/>
<Button
android:backgroundTint="#color/darkRed"
android:textColor="#android:color/white"
android:id="#+id/declineJob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Decline"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
I've tried various things but nothing seems to be working.
Try adding onResume, like this:
mapView.onCreate(null);
mapView.getMapAsync(this);
mapView.onResume();
Edit:
I've just noticed you are using
map:liteMode="true"
Try removing it (at least to test) or adding the following to the xml:
map:cameraZoom="15"
map:mapType="normal"
Either way, I think the onResume is needed.
When I use liteMode it sometimes takes a few seconds (about 5) for the map to show after the logo.
There might another issue in your code. The "map" should be:
xmlns:map="http://schemas.android.com/apk/res-auto"
but not:
xmlns:map="http://schemas.android.com/tools"

Add an image for Android marker info window

I'm making an android app. In this app, there are markers of some hotels and when user click on the marker info window appears and it should be like following image. (This is a web application)
I have a list view in my navigation drawer fragment and map fragment has the markers.
This is my code before I try to load image for the info window in MainActivity.java.
// Load Hotels
private class GetHotelTask extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... data) {
String identifier = data[0];
// Toast.makeText(getContext(), "Manuli "+identifier, Toast.LENGTH_LONG ).show();
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://api.ayubo.lk/android/retrievehotels");
HttpResponse response = null;
String responseStr = null;
try {
//add data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("long", data[0]));
nameValuePairs.add(new BasicNameValuePair("lat", data[1]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//execute http post
response = httpclient.execute(httppost);
responseStr = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return responseStr;
}
protected void onPostExecute(String result) {
try {
JSONArray jObj = new JSONArray(result);
for(int a=0;a<jObj.length();a++){
JSONArray obj= (JSONArray) jObj.get(a);
Log.i("json", obj.get(3).toString());
Marker marker = GoogleMapsFragment.map.addMarker(new MarkerOptions()
.position(new LatLng( Double.valueOf(obj.get(3).toString()), Double.valueOf(obj.get(2).toString())))
.title(obj.get(1).toString()+" | simple Price : "+obj.get(4).toString())
.snippet(obj.get(5).toString())
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_marker_hotel)));
hotelMarkers.add(marker);
}
LatLng latLng = new LatLng(Double.valueOf(latitude),Double.valueOf(longtitude));
GoogleMapsFragment.map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
GoogleMapsFragment.map.animateCamera(CameraUpdateFactory.zoomTo(15));
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
}
}
Here is the listview call of hotels inside MainActivity.java.
// Hotels
case 0:
if (A == 0){
Bundle bundle = new Bundle();
bundle.putString("restaurants", "Default");
fragmentObj = new GoogleMapsFragment();
fragmentObj.setArguments(bundle);
if (!sclItems.contains(0)){
new GetHotelTask().execute(longtitude, latitude);
sclItems.add(0);
}else {
removeHotels();
sclItems.remove((Integer)0);
}
A = 1;
}else {
if (!sclItems.contains(0)){
new GetHotelTask().execute(longtitude, latitude);
sclItems.add(0);
}else {
removeHotels();
sclItems.remove((Integer)0);
}
}
break;
I've tried adding info_window_layout.xml and here is the code.
<?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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:adjustViewBounds="true"
android:src="#drawable/ic_prof"/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/snippet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"/>
</LinearLayout>
</LinearLayout>
But, I cannot load my info window like in the image. I've tried several solutions including this and any of that didn't give me a solution.
Can someone please help me with this?
Thanks in advance. :)
Error -
Figured it out finally. :D
Changed the info_window_layout.xml like this.
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000" >
<LinearLayout
android:id="#+id/text_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/markerImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
Then added a new class called, ManiInfoAdapter.java
class MapInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
private LayoutInflater inflater;
private Context context;
public MapInfoWindowAdapter(Context context){
inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
this.context = context;
}
#Override
public View getInfoWindow(Marker marker) {
// Getting view from the layout file
View v = inflater.inflate(R.layout.info_window_layout, null);
TextView title = (TextView) v.findViewById(R.id.title);
title.setText(marker.getTitle());
TextView address = (TextView) v.findViewById(R.id.distance);
address.setText(marker.getSnippet());
ImageView iv = (ImageView) v.findViewById(R.id.markerImage);
iv.setImageResource(R.drawable.ic_attraction1);
return v;
}
#Override
public View getInfoContents(Marker marker) {
return null;
}
}
Now working as expected. :)
Try this code :
Marker kiel=FragmentLeft.map.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(p.getLatitude()), Double.parseDouble(p.getLongitude())))
.title(p.getEventName()).snippet(getLocationStringAddress(new LatLng(Double.parseDouble(p.getLatitude()), Double.parseDouble(p.getLongitude()))))
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.map_set_marker)));
markers.put(kiel.getId(),"http://"+strPhoto);
kiel.setInfoWindowAnchor(1.95f,0.0f);
hMarker.put(kiel,p);
map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker arg0) {
View myContentView = flipscreen.this.getLayoutInflater().inflate(R.layout.custommarker, null);
TextView tvTitle = ((TextView) myContentView.findViewById(R.id.title));
tvTitle.setText(arg0.getTitle());
TextView tvSnippet = ((TextView) myContentView.findViewById(R.id.snippet));
tvSnippet.setText(arg0.getSnippet());
ImageView imgMarkerImage=(ImageView)myContentView.findViewById(R.id.imgMarkerImage);
System.out.println("List of Event Photos = " + strPhoto);
Picasso.with(flipscreen.this).load(markers.get(arg0.getId())).into(imgMarkerImage);
return myContentView;
}
#Override
public View getInfoContents(Marker marker) {
return null;
}
});
map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
Iterator myVeryOwnIterator = hMarker.keySet().iterator();
while(myVeryOwnIterator.hasNext()) {
Marker key=(Marker)myVeryOwnIterator.next();
if(marker.getId().equals(key.getId())){
Event value=(Event)hMarker.get(key);
Activity_Event_Details.setEventDetails(value);
startActivity(new Intent(flipscreen.this,Activity_Event_Details.class));
}
}
}
});
I have tried to do the same for my app. I couldn't load images into the marker as I wanted.
So what I did was.
Created an xml layout maps_info_window_blue
I included into my map layout.
<fragment
android:id="#+id/map_booking_frg"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/center_marker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/shadowView"
android:layout_centerHorizontal="true"
android:src="#drawable/marker_start" />
<include layout="#layout/maps_info_window_blue"
android:id="#+id/rl_info_window_blue"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/shadowView"
android:layout_height="wrap_content"/>
<View
android:id="#+id/shadowView"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:background="#color/transparent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="invisible" />
For me the requirement was to have the marker info window in the center. You can set it where ever you want.
In order to give the illusion of a marker, I always set LatLng which I want to be the center of the map (CameraPosition target is set to the LatLng I want)
Try using info window adapter. This may help you i think.
mMap.setInfoWindowAdapter(setMarkerWindow());
private GoogleMap.InfoWindowAdapter setMarkerWindow() {
return new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
View myContentView = getLayoutInflater().inflate(
R.layout.map_info_window, null);
myContentView.setBackgroundColor(Color.TRANSPARENT);
TextView tvTitle = ((TextView) myContentView
.findViewById(R.id.title));
tvTitle.setText(marker.getTitle());
......
return myContentView;
}
#Override
public View getInfoContents(Marker marker) {
return null;
}
};
}

Hide RelativeLayout when I touch inside a map

In my android app, I have a google maps v2 inside a fragment with marker of places. When I touch in a marker, it displays a RelativeLayout with the name of the marker. However, I would like that when I touch anywhere in the map, this RelativeLayout is hidden.
My code is this:
fragment_mapa.xml
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:gravity="center" />
<RelativeLayout
android:id="#+id/sliding_up"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#fff"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:clickable="true"
android:focusable="false"
android:animateLayoutChanges="true"
android:visibility="invisible" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="14sp"
android:gravity="center_vertical"
android:paddingLeft="10dp"/>
</RelativeLayout>
Code where it creates the markers and onClick method to display the RelativeLayout
public void addItemsToMap() {
appState.mapa.clear();
if (appState.lista.isEmpty()) {
appState.readPlaces(5000, 0, appState.idCategoria);
}
appState.mapa.setOnMarkerClickListener(this);
appState.mapa.setOnInfoWindowClickListener(getInfoWindowClickListener());
LatLng miPosicion = new LatLng(obtLatitud, obtLongitud);
appState.mapa.addMarker(new MarkerOptions()
.position(miPosicion)
.title("Mi posiciĆ³n")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon)));
for (int i = 0; i < appState.lista.size(); i++) {
LatLng posItem = new LatLng(appState.lista.get(i).latitud,appState.lista.get(i).longitud);
appState.mapa.addMarker(new MarkerOptions()
.position(posItem)
.title(appState.lista.get(i).nombre)
.snippet(appState.lista.get(i).descripcion)
/*.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))*/);
Log.v("MAPA", "Marker " + i + ": " + appState.lista.get(i).nombre);
}
}
#Override
public boolean onMarkerClick(final Marker marker) {
if(marker != null) {
//marker.showInfoWindow();
RelativeLayout slideLayout;
slideLayout = (RelativeLayout) findViewById(R.id.sliding_up);
slideLayout.setVisibility(View.VISIBLE);
Animation slide = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
slideLayout.startAnimation(slide);
TextView t;
t = (TextView) findViewById(R.id.name);
t.setText(marker.getTitle());
return true;
} else {
RelativeLayout slideLayout;
slideLayout = (RelativeLayout) findViewById(R.id.sliding_up);
slideLayout.setVisibility(View.INVISIBLE);
return false;
}
}
// try this :
map.setOnMapClickListener(new OnMapClickListener() {
#Override
public void onMapClick(LatLng arg0) {
}
});

how to change the custom InfoWidnow shape of the google maps api v2

now i displayed the default rectangle shape using this code.
this.infoWindow = (ViewGroup)getLayoutInflater().inflate(R.layout.newcustomdialog, null);
this.infoImage=(ImageView)infoWindow.findViewById(R.id.graphicimage);
this.infoTitle = (TextView)infoWindow.findViewById(R.id.balloon_item_title);
this.infoSnippet = (TextView)infoWindow.findViewById(R.id.balloon_item_snippet);
this.close=(Button)infoWindow.findViewById(R.id.close_img_button);
this.infoButton = (Button)infoWindow.findViewById(R.id.more);
//
// Setting custom OnTouchListener which deals with the pressed state
// so it shows up
this.infoButtonListener = new OnInfoWindowElemTouchListener(HomeScreen.this,infoButton)
{
#Override
protected void onClickConfirmed(View v, Marker marker) {
// v.setVisibility(View.GONE);
// Here we can perform some action triggered after clicking the button
Toast.makeText(HomeScreen.this, marker.getTitle() + "'s button clicked!", Toast.LENGTH_SHORT).show();
}
};
//oraii
this.exitButtonListener=new OnInfoWindowExitListener(HomeScreen.this,infoWindow) {
#Override
protected void onClickConfirmed(View v, Marker marker) {
// TODO Auto-generated method stub
}
};
this.infoButton.setOnTouchListener(infoButtonListener);
this.close.setOnTouchListener(exitButtonListener);
map.setInfoWindowAdapter(new InfoWindowAdapter() {
public View getInfoWindow(Marker marker) {
return null;
}
public View getInfoContents(Marker marker) {
// Setting up the infoWindow with current's marker info
StringTokenizer st2 = new StringTokenizer(marker.getTitle(), ",");
String imageurl="";
String title="";
String eventid="";
while (st2.hasMoreElements()) {
eventid=st2.nextElement().toString();
imageurl=st2.nextElement().toString();
title=st2.nextElement().toString();
}
EventId=eventid;
infoTitle.setText(title);
infoSnippet.setText(marker.getSnippet());
imageLoader.DisplayImage(imageurl,HomeScreen.this, infoImage);
infoButtonListener.setMarker(marker);
exitButtonListener.setMarker(marker);
// We must call this to set the current marker and infoWindow references
// to the MapWrapperLayout
mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow);
return infoWindow;
}
});
i want to change the shape to heart shape means custom shape like a chart dialog shape..how to do that one .. plz help me if any body knows.
custom_infowindow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="110dp"
android:layout_height="110dp"
android:orientation="vertical"
android:background="#drawable/heart"
android:gravity="center"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="40dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="60dp"
android:layout_marginTop="15dp"
android:text="Click!"
android:textColor="#ffffff"
android:textSize="10dp"
android:background="#373737"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HELLO"
android:textColor="#FF0000"
android:padding="10dp"
android:layout_centerInParent="true"
android:background="#00000000"
/>
</RelativeLayout>
</LinearLayout>
this is your heart shape layout..copy and paste it in drable folder of your application...lolz
Download it
just inflate the above view into custom info window..!
mMap.setInfoWindowAdapter(new InfoWindowAdapter() {
public View getInfoWindow(Marker arg0) {
View v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
return v;
}
public View getInfoContents(Marker arg0) {
//View v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
return null;
}
});
this is just image i have created..you can add image better than me...if this works let me know...:)

Categories

Resources