单击InfoWindow android时创建一个调用活动

时间:2017-01-19 06:52:48

标签: android google-maps firebase-realtime-database

我正在开发一个Android应用程序,我可以在谷歌地图上放置标记,并显示infowindow我在哪里显示电话号码。用户

当我点击infowindow时,我想打电话给那个特定的电话号码。我曾经使用过setOnInfoWindowClickListner但它只调用一个用户

代码如下:

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
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.MarkerOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.Map;
//import static com.theakshaynaik.pocketambulance.R.id.address;

public class MapsActivity extends FragmentActivity implements      OnMapReadyCallback
{
// flag for Internet connection status
Boolean isInternetPresent = false;

// Connection detector class
ConnectionDetector cd;

// Alert Dialog Manager
AlertDialogManager alert = new AlertDialogManager();

// GPS Location
GPSTracker gps;

final int[] count = {0};
double lat,log;


final ArrayList<String> locationData = new ArrayList<>();
final ArrayList<String> DriverInfo = new ArrayList<>();




public GoogleMap mMap;

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

    cd = new ConnectionDetector(getApplicationContext());

    // Check if Internet present
    isInternetPresent = cd.isConnectingToInternet();
    if (!isInternetPresent) {
        // Internet Connection is not present
        alert.showAlertDialog(MapsActivity.this, "Internet Connection Error",
                "Please connect to working Internet connection", false);
        // stop executing code by return
        return;
    }

    // creating GPS Class object
    gps = new GPSTracker(this);

    // check if GPS location can get
    if (gps.canGetLocation()) {
        Log.d("Your Location", "latitude:" + gps.getLatitude() + ", longitude: " + gps.getLongitude());
    } else {
        // Can't get user's current location
        alert.showAlertDialog(MapsActivity.this, "GPS Status",
                "Couldn't get location information. Please enable GPS",
                false);
        // stop executing code by return
        return;
    }
    // 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);


    //Retrive data from firebase

    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference myRef = database.getReference().child("users");

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot)
        {
            collectLocation((Map<String, Object>) dataSnapshot.getValue());
            collectdriverInfo((Map<String, Object>) dataSnapshot.getValue());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }

    });
}

   //Retrive location from database
    public void collectLocation(Map<String, Object> users)
    {


        for (Map.Entry<String, Object> entry : users.entrySet())
        {
            //Get user map
            Map singleUser = (Map) entry.getValue();
            //Get phone field and append to list
            locationData.add((String) singleUser.get("location"));

            count[0]++;

        }

        System.out.println("Location: " + locationData.toString());
        System.out.println("My count is: " + count[0]);
    }

    //Retrive driver info from database

    public void collectdriverInfo(Map<String, Object> users)
    {
        final String [] userName = new String[count[0]];
        final String [] userContact = new String[count[0]];
        final double [] latitude = new double[count[0]];
        final double [] longitude = new double[count[0]];


        for (Map.Entry<String, Object> entry : users.entrySet())
        {
            //Get user map
            Map singleUser = (Map) entry.getValue();
            //Get phone field and append to list
            DriverInfo.add((String) singleUser.get("driverInfo"));
        }
        System.out.println("Driver Information: " + DriverInfo.toString());


        //Convert location string to double
        int j = 0;
        for(String loc : locationData) {
            System.out.println("LocationMMdm" + loc);
            String[] Location = loc.split(",");
            latitude[j] = Double.parseDouble(Location[0]);
            longitude[j] = Double.parseDouble(Location[1]);
            System.out.println("lat: " + latitude + " " + "log: " + longitude);

            j++;
        }

        //Split driver info and add marker on map

        int i = 0;
        for(String data : DriverInfo)
        {
            System.out.println("datainfo" + data);
            String[] singleEntry = data.split(",");
            userName[i] = singleEntry[0];
            userContact[i] = singleEntry[1];


             mMap.addMarker(new MarkerOptions().position(new LatLng(latitude[i], longitude[i])).
                    title(userName[i]).
                    snippet(userContact[i]).
                    flat(true).
                    alpha(0.9f).
                    anchor(0.5f, 0.5f).
                    rotation((0.0f)).
                    icon(BitmapDescriptorFactory.fromResource(R.drawable.markeramb)));
            i++;
        }
        System.out.println("Users name are: " + userName.toString());
        System.out.println("Users count is: " + userContact.toString());
        System.out.println("Count is:" + count[0]);
    }



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

  }
}

1 个答案:

答案 0 :(得分:0)

您可以使用示例@ http://android-er.blogspot.in/2013/01/create-custom-info-contents-for-by.html

的以下链接创建自定义信息窗口

轻松处理自定义创建信息窗口的点击事件。