Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help from someone who knows how to use maps in Android Studio. I'm interested in how to open a G.Map by clicking on a listview item from Fragment.This is my code:

This is code from Fragment:

Java
@OnItemClick(R.id.lvTours)
    public void onItemClick(int position) {


        String tour = ((Tours) lvTours.getAdapter().getItem(position)).getCoord();//coordinats are stored on Firebase
        Intent showMap = new Intent(getContext(), MapActivity.class);
        showMap.putExtra(MapActivity.COORD, longitude);
        showMap.putExtra(MapActivity.COORD, latitude);
        showMap.putExtra(MapActivity.COORD, tour);
        startActivity(showMap);

    }


And this is MapActivity:

Java
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback, LocationSource.OnLocationChangedListener {


    public static final String COORD = "coord";
    private GoogleMap.OnMapClickListener mCustomOnMapClickListener;

    private GoogleMap mGoogleMap;
    LocationManager mLocationManager;
    private Criteria mCriteria;
    private String mShowMap;


    private LocationSource.OnLocationChangedListener mListener;
    @BindView(R.id.lvTours)
    ListView lvTours;
    private SupportMapFragment mSupportMapFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tours_coord);
        this.mSupportMapFragment = (SupportMapFragment) this.getSupportFragmentManager().findFragmentById(R.id.fGoogleMap);
        this.mSupportMapFragment.getMapAsync(this);
        this.mCriteria = new Criteria();
        this.mCriteria.setAccuracy(Criteria.ACCURACY_FINE);
        this.mLocationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
        this.mCustomOnMapClickListener = new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                MarkerOptions newMarkerOptions = new MarkerOptions();
                newMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.tour));
                newMarkerOptions.title("Tour");
                newMarkerOptions.snippet("It' was here!");
                newMarkerOptions.position(latLng);
                mGoogleMap.addMarker(newMarkerOptions);

                this.handleStartingIntent();

            }

            private void handleStartingIntent() {
                Bundle bundle = getIntent().getExtras();
                if (bundle.containsKey(COORD)) {
                    mShowMap = bundle.getString(COORD);

                }


            }
        };
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        this.mGoogleMap = googleMap;
        UiSettings uiSettings = this.mGoogleMap.getUiSettings();
        uiSettings.setZoomControlsEnabled(true);
        uiSettings.setMyLocationButtonEnabled(true);
        uiSettings.setZoomGesturesEnabled(true);
        this.mGoogleMap.setOnMapClickListener(this.mCustomOnMapClickListener);
        // goToLocation(33.835293 , -117.914505);
    }


    @Override
    public void onLocationChanged(Location location) {


        if (mListener != null) {
            mListener.onLocationChanged(location);

            //Move the camera to the user's location and zoom in!
            mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 12.0f));
        }


    }


    public void goToLocation(double lat, double lng) {

        LatLng latLng = new LatLng(lat, lng);


        CameraPosition position = CameraPosition.builder()
                .target(latLng)
                .zoom(16f)
                .bearing(0.0f)
                .tilt(0.0f)
                .build();

        mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), null);


    }
}


I would be grateful to anyone who would make me an example on how to solve this.Thanks.

What I have tried:

It tried to found a solution using Google.
Posted
Comments
David Crow 9-Jan-18 10:37am    
What exactly is happening? Is an exception being thrown? Does a map open but it is the wrong location? Be specific.

I see that you are storing latitude, longitude and tour all using the same key. Then when you are reading that key in handleStartingIntyent(), you don't do anything with it. Is that intentional.
Member 13593129 10-Jan-18 5:32am    
It's not intentional, but I'll try your code that you provided, and let you know.
Member 13593129 10-Jan-18 9:21am    
It doesn't work. I get NP on "private double dLongitude = 0.0;" and "setContentView(R.layout.tours_coord);". I'll post how it looks now.

public class MapActivity extends FragmentActivity implements OnMapReadyCallback {


private double dLatitude = 0.0;
private double dLongitude = 0.0;

public static final String COORD = "coord";
private String mTours;

private GoogleMap mMap;
private GoogleMap.OnMapClickListener mCustomOnMapClickListener;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tours_coord);

// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fGoogleMap);
mapFragment.getMapAsync(this);

Bundle bundle = getIntent().getExtras();
dLatitude = bundle.getDouble("LATITUDE");
dLongitude = bundle.getDouble("LONGITUDE");
if (bundle.containsKey(COORD)) {
mTours = bundle.getString(COORD);
}
}

@Override
public void onMapReady(GoogleMap googleMap) {

mMap = googleMap;

LatLng latlng = new LatLng(dLatitude, dLongitude);
mMap.addMarker(new MarkerOptions().position(latlng).title("It was here"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
this.mCustomOnMapClickListener = new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
MarkerOptions newMarkerOptions = new MarkerOptions();
newMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.tour));
newMarkerOptions.title("Tour");
newMarkerOptions.snippet("It' was here!");
newMarkerOptions.position(latLng);
mMap.addMarker(newMarkerOptions);


}
};
}
}



@OnItemClick(R.id.lvTours)
public void onItemClick(int position) {

String t = ((Tours)lvTours.getAdapter().getItem(position)).getCoord();
Intent i = new Intent(getContext(), MapActivity.class);
i.putExtra("LATITUDE", 33.835293);
i.putExtra("LONGITUDE", -117.914505);
i.putExtra(MapActivity.COORD, t);
startActivity(i);

}

David Crow 10-Jan-18 10:45am    
"It doesn't work. I get NP on 'private double dLongitude = 0.0;'"

I'm not sure how that's possible since there are no objects involved. Is a NullpointerException exception being thrown? How are you verifying this?

The call to setContentView() is fine (again, no objects), but something internally might have gone awry when the framework attempted to inflate the tours_coord layout.
Member 13593129 10-Jan-18 11:36am    
I was being a idiot. I forgot to put API_KEY, that's why it didn't work. Although, I have one more question.It works, it shows just one location, though.I want when I click on every item, to show other place. Cause I have 27 tours, and they weren't on same place. Do you have an idea how to do that?


1 solution

The minimum amount of code I can think of to open a map looks like:
activity_maps.xml:

<fragment 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:id="@+id/map"
          android:name="com.google.android.gms.maps.SupportMapFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        Intent intent = new Intent(this, MapsActivity.class);
        intent.putExtra("LATITUDE", 38.65452);
        intent.putExtra("LONGITUDE", -90.18471);
        startActivity(intent);
    }
}

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback
{
    private double dLatitude = 0.0;
    private double dLongitude = 0.0;

    private GoogleMap mMap;

    //================================================================

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        Bundle bundle = getIntent().getExtras();
        dLatitude = bundle.getDouble("LATITUDE");
        dLongitude = bundle.getDouble("LONGITUDE");

        // 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);
    }

    //================================================================

    @Override
    public void onMapReady( GoogleMap googleMap )
    {
        mMap = googleMap;

        LatLng latlng = new LatLng(dLatitude, dLongitude);
        mMap.addMarker(new MarkerOptions().position(latlng).title("Marker"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
    }
}
You can add to it from here.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900