0
votes

I'm developing an app using Google Maps. I have created some markers with certain information (e.g. store title, store address, phone number, etc...). When I click on a marker, an Info Window opens with the required information, however I want to display each marker's information in a new activity (DetailedInformation.java) when I click on Info Window. I'm using Intent for a new activity. Below is my code:

GoogleMapsFragment.java

package com.user.sa.someapp;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
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.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
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 GoogleMapsFragment extends Fragment implements OnMapReadyCallback {

private final static int MY_PERMISSION_FINE_LOCATION = 101;

GoogleMap map;

public GoogleMapsFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_google_maps, container, false);
    return v;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    SupportMapFragment mapFragment = (SupportMapFragment)
            getChildFragmentManager().findFragmentById(R.id.map1);
    mapFragment.getMapAsync(this);
}

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

    if (map != null) {
        map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter(){

            @Override
            public View getInfoWindow(Marker marker) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {
                View v = getActivity().getLayoutInflater().inflate(R.layout.infowindow_layout, null);
                TextView storeTitle = (TextView) v.findViewById(R.id.store_title);
                TextView storeSnippet = (TextView) v.findViewById(R.id.store_snippet);
                storeTitle.setText(marker.getTitle());
                storeSnippet.setText(marker.getSnippet());
                return v;
            }
        });
    }

    map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            Intent intent = new Intent(GoogleMapsFragment.this.getActivity(), DetailedInformation.class);
            startActivity(intent);
        }
    });

    // MARKERS

    LatLng store1 = new LatLng(55.000000, 21.000000);
    map.addMarker(new MarkerOptions().position(store1)
            .title("SOME STORE")
            .snippet("Cool Store \n\n Store address 1, \n\n +370 000 00000 \n\n store1@store1 \n www.example.com")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
    );
    map.moveCamera(CameraUpdateFactory.newLatLng(store1));

    if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        map.setMyLocationEnabled(true);
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_FINE_LOCATION);
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case MY_PERMISSION_FINE_LOCATION:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    map.setMyLocationEnabled(true);
                }
            }
            break;
    }
}
}
1
You just want to pass the title and snippet to the new Activity when clicking an InfoWindow?Daniel Nugent
I get the question but I dont really get the problem. The new activity does not open or..?nada
@DanielNugent Yes, I want to pass the title and snippet to the new activity. When I click the Info Window a blank activity opens. I want to display marker's information on itPIOTR CYBULSKIJ

1 Answers

0
votes
map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
    @Override
    public void onInfoWindowClick(Marker marker) {
        Intent intent = new Intent(GoogleMapsFragment.this.getActivity(), DetailedInformation.class);
        intent.putExtra("title", marker.getTitle());
        intent.putExtra("snippet", marker.getSnippet());
        startActivity(intent);
    }
});

In your Detail activity's onCreate method:

String title = getIntent().getStringExtra("title");
String snippet = getIntent().getStringExtra("snippet");