Skip to content Skip to sidebar Skip to footer

How Can Get The Information For Place On Google Map?

I want to use information from Google when I search restaurant or some place i tried to make start an activity when the marker in Google Maps is clicked so use the onMarkerClick bu

Solution 1:

You want to show more data in marker info window so you need to put some data in marker when it create .you should make some new things like that: 1. Firstly create a hash map to put the value.

 HashMap<String, String> map = new HashMap<>();
        map.put("userId", userId);
        map.put("userName", name);
        if(profileImage.equalsIgnoreCase(""))
        map.put("imageUrl", null);
        elsemap.put("imageUrl", profileImage);
        map.put("primaryInstrumentName", primary_Instrument_name);

        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(Double.valueOf(latitude), Double.valueOf(lonnitude))).snippet(String.valueOf(map))
                .icon(R.drawable.marker);

when u set listner on google map info window its return the marker object get marker.getsnippet which return the hashmap type string convert it in single key value according to this code:

privateMap<String, String> getMarkerDetails(Marker marker) {
        String name2 = marker.getSnippet();
        System.out.println("name  "+name2);

        name2 = name2.substring(1, name2.length() - 1);
        String[] keyValuePairs = name2.split(",");
        Map<String, String> map = newHashMap<>();

        for (String pair : keyValuePairs) {
            String[] entry = pair.split("=");
            System.out.println("name map is "+entry[0]+" "+entry[1]);
            map.put(entry[0].trim(), entry[1].trim());
        }
        return map;
    }

this would help to get back key value from string hashmap using this like:

getMarkerDetails(marker).get("userName").toString();

this will return to marker "username" data , use wherever need.

Solution 2:

Try this full example of custom marker.

Map Activity :

publicvoidonMapReady(GoogleMap googleMap) { 
mMap.clear();
LinearLayout tv2;
tv2 = (LinearLayout)MapsActivity.this.getLayoutInflater().inflate(R.layout.marker_dialog, 
null, false);
tv2.measure(View.MeasureSpec.makeMeasureSpec(0, 
View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, 
View.MeasureSpec.UNSPECIFIED));
tv2.layout(0, 0, tv2.getMeasuredWidth(), tv2.getMeasuredHeight());
tv2.setDrawingCacheEnabled(true);
tv2.buildDrawingCache();
Bitmapbm2= tv2.getDrawingCache();
icon2 = BitmapDescriptorFactory.fromBitmap(bm2);
BitmapDescriptorFactory.fromResource(R.drawable.ic_markericon);

HashMap<Marker, JSONObject> stopsMarkersInfo = newHashMap<>();
JSONObject ObjectForMarker;
for (inti=0; i < YourData.length(); i++) 
{//YourData is JSONArrayLatLngcoordinate=newLatLng(Latitude, Longitude);
  ObjectForMarker = YourData.getJSONObject(i);
  MarkerOptionsmarkerOptions=newMarkerOptions().position(coordinate).icon(Youricon);
  Markermarker= mMap.addMarker(markerOptions);
  stopsMarkersInfo.put(marker, ObjectForMarker); 
}
 googleMap.setInfoWindowAdapter(newStopsInfoWindow(stopsMarkersInfo, getApplicationContext()));
     }

Custom Adapter Class :

import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.widget.TextView;
  import com.google.android.gms.maps.GoogleMap;
  import com.google.android.gms.maps.model.Marker;
  import org.json.JSONException;
  import org.json.JSONObject;
  import java.util.HashMap;
  publicclassStopsInfoWindowimplementsGoogleMap.InfoWindowAdapter {

privateHashMap<Marker, JSONObject> stopsMarkersInfo;
privateView view;
Context context;

publicStopsInfoWindow(HashMap<Marker, JSONObject> stopsMarkersInfo, Context context) {
    this.stopsMarkersInfo = stopsMarkersInfo;
    this.context = context;
}


@OverridepublicViewgetInfoContents(Marker marker) {
    returnnull;
}

@OverridepublicViewgetInfoWindow(final Marker marker) {
    JSONObject stop = stopsMarkersInfo.get(marker);
    if (stop != null) {
        LayoutInflater inflater = (LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        view = inflater.inflate(R.layout.item_stop_marker_info, null);

        TextView iD = (TextView) view.findViewById(R.id.iD);
        TextViewAmount = (TextView) view.findViewById(R.id.Amount);
        TextViewDate = (TextView) view.findViewById(R.id.Date);



        for (int i = 0; i < stopsMarkersInfo.size(); i++) {

         }

        try {
            iD.setText(stop.getString("iD"));
            Amount.setText(stop.getString("Amount"));
            Date.setText(stop.getString("Date"));


        } catch (JSONException e) {
            e.printStackTrace();
        }


    }
    return view;
  }
  }

marker_dialog.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/myMarker"android:layout_width="100dp"android:layout_height="wrap_content"android:background="@android:color/transparent"android:orientation="vertical"><ImageViewandroid:layout_width="50dp"android:layout_height="60dp"android:layout_gravity="center_horizontal"android:src="@drawable/ic_markericon" /></LinearLayout>

item_stop_marker_info.xml : Declare all the options you want in your marker

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/roundlayout"><TextViewandroid:id="@+id/iD"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_toRightOf="@+id/MName"android:text="iD"android:textColor="@color/background"android:textSize="13dp" /><TextViewandroid:layout_marginRight="10dp"android:id="@+id/Amount"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Amount"android:textColor="@color/background"android:textSize="13dp" /></RelativeLayout>

Post a Comment for "How Can Get The Information For Place On Google Map?"