Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to add marker with Infowindow in google map?

    • 0
    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 324
    Comment on it

    In the below example code I have described to add a  marker with Infowindow. Here I have used array list for adding marker items, then I have created plotMarkers method, in this method, it will addMarker by passing MarkerOptions as a parameter. I have used LatLng to define the position to add the marker. In next step, I have used setInfoWindowAdapter method and in getInfoContents I have inflated window Layout for CustomWindow layout.

     

    public class MapsActivity extends Activity {
        private GoogleMap mMap;
        private ArrayList<MyMarker> mMyMarkersArray = new ArrayList<MyMarker>();
        private HashMap<Marker, MyMarker> mMarkersHashMap;
        ImageView image;
        MyMarker myMarker;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
            
            // Initialize the HashMap for Markers and MyMarker object
            mMarkersHashMap = new HashMap<Marker, MyMarker>();
            mMyMarkersArray.add(new MyMarker("Kanpur", "icon1", Double.parseDouble("26.44721"), Double.parseDouble("80.32242")));
            mMyMarkersArray.add(new MyMarker("United States", "icon2", Double.parseDouble("33.7266622"), Double.parseDouble("-87.1469829")));
            mMyMarkersArray.add(new MyMarker("Canada", "icon3", Double.parseDouble("51.8917773"), Double.parseDouble("-86.0922954")));
            mMyMarkersArray.add(new MyMarker("England", "icon4", Double.parseDouble("52.4435047"), Double.parseDouble("-3.4199249")));
            mMyMarkersArray.add(new MyMarker("Espaa", "icon5", Double.parseDouble("41.8728262"), Double.parseDouble("-0.2375882")));
            mMyMarkersArray.add(new MyMarker("Portugal", "icon6", Double.parseDouble("40.8316649"), Double.parseDouble("-4.936009")));
            mMyMarkersArray.add(new MyMarker("Deutschland", "icon7", Double.parseDouble("51.1642292"), Double.parseDouble("10.4541194")));
            mMyMarkersArray.add(new MyMarker("Atlantic Ocean", "icondefault", Double.parseDouble("-13.1294607"), Double.parseDouble("-19.9602353")));
            setUpMap();
            plotMarkers(mMyMarkersArray);
        }
    
        private void plotMarkers(ArrayList<MyMarker> markers) {
            if (markers.size() > 0) {
                for (MyMarker myMarker : markers) {
                    // Create user marker with custom icon and other options
                    MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
                    markerOption.icon(BitmapDescriptorFactory.fromResource(R.drawable.one));
                    Marker currentMarker = mMap.addMarker(markerOption);
                    mMarkersHashMap.put(currentMarker, myMarker);
                    mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
                }
            }
        }
    
                    // Added infoWindow icons
        private int markerIcon(String markerIcon) {
            if (markerIcon.equals("icon1"))
                return R.drawable.mba;
            else if (markerIcon.equals("icon2"))
                return R.drawable.mba;
            else if (markerIcon.equals("icon3"))
                return R.drawable.mba;
            else if (markerIcon.equals("icon4"))
                return R.drawable.mba;
            else if (markerIcon.equals("icon5"))
                return R.drawable.mba;
            else if (markerIcon.equals("icon6"))
                return R.drawable.mba;
            else if (markerIcon.equals("icon7"))
                return R.drawable.mba;
            else
                return R.drawable.mba;
        }
        private void setUpMap() {
            // Do a null check to confirm that we have not already instantiated the map.
            if (mMap == null) {
                // Try to obtain the map from the SupportMapFragment.
                mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
                // Check if we were successful in obtaining the map.
                if (mMap != null) {
                    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                        @Override
                        public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker) {
                            marker.showInfoWindow();
                            return true;
                        }
                    });
                } else
                    Toast.makeText(getApplicationContext(), "Unable to create Maps", Toast.LENGTH_SHORT).show();
            }
        }
           public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
            public MarkerInfoWindowAdapter() {
            }
            @Override
            public View getInfoWindow(Marker marker) {
     
                return null;
            }
            @Override
            public View getInfoContents(Marker marker) {
                View v = getLayoutInflater().inflate(R.layout.window, null);   //inflate window layout
                MyMarker myMarker = mMarkersHashMap.get(marker);
                ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);
                TextView markerLabel = (TextView) v.findViewById(R.id.marker_label);
                markerIcon.setImageResource(markerIcon(myMarker.getmIcon()));     //set icons and text by using bin class
                markerLabel.setText(myMarker.getmLabel());                      
                return v;
            }
        }
    }
    
    

     

 2 Comment(s)

  • hii, To search marker based on mLabel value and automat show infowindow, See below code-

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    
        private GoogleMap mMap;
        MarkerOptions markerOptions;
        LatLng latLng;
        private GoogleMap.OnInfoWindowClickListener infoWindowClickListener;
        private HashMapmarker;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
            // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
            mMap=mapFragment.getMap();
    
            mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                private View view;
    
                @Override
                public View getInfoWindow(Marker marker) {
                    return view;
                }
    
                @Override
                public View getInfoContents(Marker marker) {
                    View v = getLayoutInflater().inflate(R.layout.windowlayout, null);
                    latLng = marker.getPosition();
                    TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
                    ImageView imageView = (ImageView) v.findViewById(R.id.marker_icon);
                    TextView address = (TextView) v.findViewById(R.id.tv_palace);
                    Button button = (Button) v.findViewById(R.id.button2);
                    TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);
    
    
                    tvLat.setText("Latitude:" + latLng.latitude);
                    tvLng.setText("Longitude:" + latLng.longitude);
                    button.setVisibility(View.VISIBLE);
    
    
                    try {
                        Geocoder geocoder = new Geocoder(MapsActivity.this.getApplicationContext(), Locale.getDefault());
                        List addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
    
                        if (addresses.isEmpty()) {
    
                        } else {
                            if (addresses.size() > 0) {
    
                                address.setText("Place Name-:" + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality());
                                Toast.makeText(getApplicationContext(), "Address:-" + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality(), Toast.LENGTH_LONG).show();
                            }
                        }
    
    
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    tvLng.setText("Longitude" + latLng.longitude);
                    button.setVisibility((View.VISIBLE));
                    return v;
    
                }
            });
            mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                @Override
                public void onInfoWindowClick(Marker marker) {
                    mMap.addMarker(new MarkerOptions().position(latLng)).setVisible(true);
                    CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
                            -73.98180484771729));
                    CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
    
                    mMap.moveCamera(center);
                    mMap.animateCamera(zoom);
    
    
                    Toast.makeText(MapsActivity.this, "You have clicked the window", Toast.LENGTH_SHORT).show();
                }
            });
            mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                @Override
                public void onMapClick(LatLng arg0) {
                    latLng=arg0;
                    mMap.clear();
    
                    MarkerOptions markerOptions= new MarkerOptions();
                    markerOptions.position(latLng);
    
                    mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
    
                    Marker marker = mMap.addMarker(markerOptions);
    
                }
            });
    
        }
        public void onMapSearch(View view){
            EditText locationSearch=(EditText)findViewById(R.id.editText);
            String location=locationSearch.getText().toString();
            ListaddressList=null;
    
            if(location!=null||!location.equals("")){
                Geocoder geocoder= new Geocoder(this);
                try{
                    addressList=geocoder.getFromLocationName(location,1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Address address= addressList.get(0);
                LatLng latLng= new LatLng(address.getLatitude(),address.getLongitude());
                mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
                mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
            }
        }
    
    
        /**
         * Manipulates the map once available.
         * This callback is triggered when the map is ready to be used.
         * This is where we can add markers or lines, add listeners or move the camera. In this case,
         * we just add a marker near Sydney, Australia.
         * If Google Play services is not installed on the device, the user will be prompted to install
         * it inside the SupportMapFragment. This method will only be triggered once the user has
         * installed Google Play services and returned to the app.
         */
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    
            CameraPosition googlePlex = CameraPosition.builder()
                    .target(new LatLng(37.4219999,-122.0862462))
                    .zoom(16)
                    .bearing(0)
                    .tilt(45)
                    .build();
    
            mMap.moveCamera(CameraUpdateFactory.newCameraPosition(googlePlex));
    
            mMap.setMyLocationEnabled(true);
        }
    }
    
    
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: