Display Streetviewpanoramaview Inwards Dialogfragment, When User Click On Infowindow

Posted on

Example alter from finally instance “Detect user click on InfoWindow, yesteryear implementing GoogleMap.OnInfoWindowClickListener()“, to open DialogFragment amongst StreetViewPanoramaView when user click on InfoWindow.


MapsActivity.java

package com.blogspot.android_er.androidstudiomapapp;  import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.app.Fragment; import android.app.FragmentTransaction; import android.content.DialogInterface; import android.content.Intent; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.InputType; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast;  import com.google.android.gms.common.GoogleApiAvailability; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.StreetViewPanoramaOptions; import com.google.android.gms.maps.StreetViewPanoramaView; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions;  populace shape MapsActivity extends AppCompatActivity implements OnMapReadyCallback,         GoogleMap.OnMapClickListener, GoogleMap.OnMapLongClickListener,         GoogleMap.OnMarkerDragListener, GoogleMap.InfoWindowAdapter {      somebody GoogleMap mMap;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_maps);         // Obtain the SupportMapFragment together with larn notified when the map is prepare to endure used.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()                 .findFragmentById(R.id.map);         mapFragment.getMapAsync(this);      }      /**      * Manipulates the map 1 time available.      * This callback is triggered when the map is prepare to endure used.      * This is where nosotros tin add together markers or lines, add together listeners or motility the camera. In this case,      * nosotros merely add together a marking close Sydney, Australia.      * If Google Play services is non installed on the device, the user volition endure prompted to install      * it within the SupportMapFragment. This method volition entirely endure triggered 1 time the user has      * installed Google Play services together with returned to the app.      */     @Override     populace void onMapReady(GoogleMap googleMap) {         mMap = googleMap;         mMap.setOnMapClickListener(this);         mMap.setOnMapLongClickListener(this);         mMap.setOnMarkerDragListener(this);         mMap.setInfoWindowAdapter(this);         mMap.setOnInfoWindowClickListener(MyOnInfoWindowClickListener);     }      @Override     populace boolean onCreateOptionsMenu(Menu menu) {         getMenuInflater().inflate(R.menu.activity_main, menu);         provide super.onCreateOptionsMenu(menu);     }      @Override     populace boolean onOptionsItemSelected(MenuItem item) {         switch (item.getItemId()) {             instance R.id.menu_addmarkers:                 addMarker();                 provide true;             instance R.id.maptypeHYBRID:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);                     provide true;                 }             instance R.id.maptypeNONE:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_NONE);                     provide true;                 }             instance R.id.maptypeNORMAL:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);                     provide true;                 }             instance R.id.maptypeSATELLITE:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);                     provide true;                 }             instance R.id.maptypeTERRAIN:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);                     provide true;                 }             instance R.id.menu_legalnotices:                 String LicenseInfo = GoogleApiAvailability                         .getInstance()                         .getOpenSourceSoftwareLicenseInfo(MapsActivity.this);                 AlertDialog.Builder LicenseDialog =                         novel AlertDialog.Builder(MapsActivity.this);                 LicenseDialog.setTitle("Legal Notices");                 LicenseDialog.setMessage(LicenseInfo);                 LicenseDialog.show();                 provide true;             instance R.id.menu_about:                 AlertDialog.Builder aboutDialogBuilder =                         novel AlertDialog.Builder(MapsActivity.this);                 aboutDialogBuilder.setTitle("About Me")                         .setMessage("https://rootandroid.org/");                  aboutDialogBuilder.setPositiveButton("visit",                         novel DialogInterface.OnClickListener() {                     @Override                     populace void onClick(DialogInterface dialog, int which) {                         String url = "https://rootandroid.org/";                         Intent i = novel Intent(Intent.ACTION_VIEW);                         i.setData(Uri.parse(url));                         startActivity(i);                     }                 });                  aboutDialogBuilder.setNegativeButton("Dismiss",                         novel DialogInterface.OnClickListener() {                     @Override                     populace void onClick(DialogInterface dialog, int which) {                         dialog.cancel();                     }                 });                  AlertDialog aboutDialog = aboutDialogBuilder.create();                 aboutDialog.show();                  provide true;         }         provide super.onOptionsItemSelected(item);     }      somebody void addMarker(){         if(mMap != null){              //create custom LinearLayout programmatically             LinearLayout layout = novel LinearLayout(MapsActivity.this);             layout.setLayoutParams(new LinearLayout.LayoutParams(                     LinearLayout.LayoutParams.MATCH_PARENT,                     LinearLayout.LayoutParams.WRAP_CONTENT));             layout.setOrientation(LinearLayout.VERTICAL);              concluding EditText titleField = novel EditText(MapsActivity.this);             titleField.setHint("Title");              concluding EditText latField = novel EditText(MapsActivity.this);             latField.setHint("Latitude");             latField.setInputType(InputType.TYPE_CLASS_NUMBER                     | InputType.TYPE_NUMBER_FLAG_DECIMAL                     | InputType.TYPE_NUMBER_FLAG_SIGNED);              concluding EditText lonField = novel EditText(MapsActivity.this);             lonField.setHint("Longitude");             lonField.setInputType(InputType.TYPE_CLASS_NUMBER                     | InputType.TYPE_NUMBER_FLAG_DECIMAL                     | InputType.TYPE_NUMBER_FLAG_SIGNED);              layout.addView(titleField);             layout.addView(latField);             layout.addView(lonField);              AlertDialog.Builder builder = novel AlertDialog.Builder(this);             builder.setTitle("Add Marker");             builder.setView(layout);             AlertDialog alertDialog = builder.create();              builder.setPositiveButton("OK", novel DialogInterface.OnClickListener() {                 @Override                 populace void onClick(DialogInterface dialog, int which) {                     boolean parsable = true;                     Double lat = null, lon = null;                      String strLat = latField.getText().toString();                     String strLon = lonField.getText().toString();                     String strTitle = titleField.getText().toString();                      try{                         lat = Double.parseDouble(strLat);                     }catch (NumberFormatException ex){                         parsable = false;                         Toast.makeText(MapsActivity.this,                                 "Latitude does non incorporate a parsable double",                                 Toast.LENGTH_LONG).show();                     }                      try{                         lon = Double.parseDouble(strLon);                     }catch (NumberFormatException ex){                         parsable = false;                         Toast.makeText(MapsActivity.this,                                 "Longitude does non incorporate a parsable double",                                 Toast.LENGTH_LONG).show();                     }                      if(parsable){                          LatLng targetLatLng = novel LatLng(lat, lon);                         MarkerOptions markerOptions =                                 novel MarkerOptions().position(targetLatLng).title(strTitle);                          markerOptions.draggable(true);                          mMap.addMarker(markerOptions);                         mMap.moveCamera(CameraUpdateFactory.newLatLng(targetLatLng));                      }                 }             });             builder.setNegativeButton("Cancel", null);              builder.show();         }else{             Toast.makeText(MapsActivity.this, "Map non ready", Toast.LENGTH_LONG).show();         }     }      @Override     populace void onMapClick(LatLng latLng) {         Toast.makeText(MapsActivity.this,                 "onMapClick:\n" + latLng.latitude + " : " + latLng.longitude,                 Toast.LENGTH_LONG).show();     }      @Override     populace void onMapLongClick(LatLng latLng) {         Toast.makeText(MapsActivity.this,                 "onMapLongClick:\n" + latLng.latitude + " : " + latLng.longitude,                 Toast.LENGTH_LONG).show();          //Add marking on LongClick seat         MarkerOptions markerOptions =                 novel MarkerOptions().position(latLng).title(latLng.toString());         markerOptions.draggable(true);          mMap.addMarker(markerOptions);     }       @Override     populace void onMarkerDragStart(Marker marker) {         marker.setTitle(marker.getPosition().toString());         marker.showInfoWindow();         marker.setAlpha(0.5f);     }      @Override     populace void onMarkerDrag(Marker marker) {         marker.setTitle(marker.getPosition().toString());         marker.showInfoWindow();         marker.setAlpha(0.5f);     }      @Override     populace void onMarkerDragEnd(Marker marker) {         marker.setTitle(marker.getPosition().toString());         marker.showInfoWindow();         marker.setAlpha(1.0f);     }      @Override     populace View getInfoWindow(Marker marker) {         provide null;         //return prepareInfoView(marker);     }      @Override     populace View getInfoContents(Marker marker) {         //return null;         provide prepareInfoView(marker);      }      somebody View prepareInfoView(Marker marker){         //prepare InfoView programmatically         LinearLayout infoView = novel LinearLayout(MapsActivity.this);         LinearLayout.LayoutParams infoViewParams = novel LinearLayout.LayoutParams(                 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);         infoView.setOrientation(LinearLayout.HORIZONTAL);         infoView.setLayoutParams(infoViewParams);          ImageView infoImageView = novel ImageView(MapsActivity.this);         //Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);         Drawable drawable = getResources().getDrawable(android.R.drawable.ic_dialog_map);         infoImageView.setImageDrawable(drawable);         infoView.addView(infoImageView);          LinearLayout subInfoView = novel LinearLayout(MapsActivity.this);         LinearLayout.LayoutParams subInfoViewParams = novel LinearLayout.LayoutParams(                 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);         subInfoView.setOrientation(LinearLayout.VERTICAL);         subInfoView.setLayoutParams(subInfoViewParams);          TextView subInfoLat = novel TextView(MapsActivity.this);         subInfoLat.setText("Lat: " + marker.getPosition().latitude);         TextView subInfoLnt = novel TextView(MapsActivity.this);         subInfoLnt.setText("Lnt: " + marker.getPosition().longitude);         subInfoView.addView(subInfoLat);         subInfoView.addView(subInfoLnt);         infoView.addView(subInfoView);          provide infoView;     }      GoogleMap.OnInfoWindowClickListener MyOnInfoWindowClickListener             = novel GoogleMap.OnInfoWindowClickListener(){         @Override         populace void onInfoWindowClick(Marker marker) {             Toast.makeText(MapsActivity.this,                     "onInfoWindowClick():\n" +                     marker.getPosition().latitude + "\n" +                     marker.getPosition().longitude,                     Toast.LENGTH_LONG).show();              showStreetViewDialog(marker.getPosition());         }     };      private void showStreetViewDialog(LatLng pos){         FragmentTransaction ft = getFragmentManager().beginTransaction();         Fragment prev = getFragmentManager().findFragmentByTag("streetview");         if (prev != null) {             ft.remove(prev);         }         ft.addToBackStack(null);          DialogFragment newFragment = StrretViewDialogFragment.newInstance(pos);         newFragment.show(ft, "streetview");     }      public static shape StrretViewDialogFragment extends DialogFragment {          LatLng latLng;          static StrretViewDialogFragment newInstance(LatLng latLng) {             StrretViewDialogFragment f = novel StrretViewDialogFragment();              Bundle args = novel Bundle();             args.putDouble("latitude", latLng.latitude);             args.putDouble("longitude", latLng.longitude);             f.setArguments(args);              provide f;         }          @Override         populace Dialog onCreateDialog(Bundle savedInstanceState) {             latLng = novel LatLng(getArguments().getDouble("latitude"),                     getArguments().getDouble("longitude"));              LinearLayout layout = novel LinearLayout(getActivity());             layout.setLayoutParams(new LinearLayout.LayoutParams(                     LinearLayout.LayoutParams.MATCH_PARENT,                     LinearLayout.LayoutParams.MATCH_PARENT));             layout.setOrientation(LinearLayout.VERTICAL);              StreetViewPanoramaOptions options=new StreetViewPanoramaOptions();             options.position(latLng);             StreetViewPanoramaView streetViewPanoramaView =                     novel StreetViewPanoramaView(getActivity(), options);             streetViewPanoramaView.setLayoutParams(                     novel LinearLayout.LayoutParams(                             LinearLayout.LayoutParams.MATCH_PARENT,                             LinearLayout.LayoutParams.MATCH_PARENT));             streetViewPanoramaView.setPadding(5, 5, 5, 5);             streetViewPanoramaView.onCreate(savedInstanceState);              layout.addView(streetViewPanoramaView);              provide novel AlertDialog.Builder(getActivity())                     .setTitle(latLng.latitude + " : "  + latLng.longitude)                     .setPositiveButton("OK", null)                     .setView(layout)                     .create();         }     }  }  

reference: https://developers.google.com/android/reference/com/google/android/gms/maps/StreetViewPanoramaView


Step-by-step of Android Google Maps Activity using Google Maps Android API v2, on Android Studio