Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Last active November 29, 2023 19:28
Show Gist options
  • Save sunmeat/e579076368145450d8e0d2a269428541 to your computer and use it in GitHub Desktop.
Save sunmeat/e579076368145450d8e0d2a269428541 to your computer and use it in GitHub Desktop.
nice example for google street view android
activity_maps.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:context="com.sunmeat.maps.MapsActivity" />
<fragment
android:id="@+id/streetviewpanorama"
android:name="com.google.android.gms.maps.StreetViewPanoramaFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
======================================================================================
MapsActivity.java:
package com.alex.maps;
import android.os.Bundle;
import android.widget.Toast;
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.OnStreetViewPanoramaReadyCallback;
import com.google.android.gms.maps.StreetViewPanorama;
import com.google.android.gms.maps.StreetViewPanoramaFragment;
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;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, OnStreetViewPanoramaReadyCallback {
private GoogleMap m;
private StreetViewPanorama panorama;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
StreetViewPanoramaFragment streetViewPanoramaFragment =
(StreetViewPanoramaFragment) getFragmentManager()
.findFragmentById(R.id.streetviewpanorama);
streetViewPanoramaFragment.getStreetViewPanoramaAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
m = map;
LatLng odessa = new LatLng(46.4825, 30.7233);
Marker marker = m.addMarker(new MarkerOptions()
.position(odessa)
.title("Odessa")
.snippet("We are here!"));
m.moveCamera(CameraUpdateFactory.newLatLng(odessa));
m.animateCamera(CameraUpdateFactory.zoomTo(5f));
m.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng ll) {
if (panorama != null) {
Toast.makeText(MapsActivity.this, ll.latitude + ", " + ll.longitude, Toast.LENGTH_SHORT).show();
panorama.setPosition(ll);
}
}
});
}
@Override
public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {
panorama = streetViewPanorama;
}
}
@asilichenko
Copy link

Instead of this:

m.moveCamera(CameraUpdateFactory.newLatLng(odessa));
m.animateCamera(CameraUpdateFactory.zoomTo(5f));

you should use this:

m.moveCamera(CameraUpdateFactory.newLatLngZoom(odessa, 5f));

Otherwise the marker is not located in the center of the screen. I don't know why but this is my case.

@sunmeat
Copy link
Author

sunmeat commented Nov 29, 2023

Thanks for the clarification, @asilichenko!

Yes, you are completely right. But this is Android... something changes all the time there :)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment