-
How to add marker with Infowindow in google map?
over 8 years ago
-
over 8 years ago
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); } }
-
-
over 8 years ago
Sir, how to search marker based on mLabel value and automat show infowindow ?
-
2 Comment(s)