2017-01-19 51 views
1

我正在開發一個android應用程序,我可以在google地圖上放置標記並顯示infowindow,我已經顯示了手機號碼。的用戶當在InfoWindow上點擊時創建一個通話活動android

當我點擊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); 

    } 
} 
+0

請參閱本文[http://stackoverflow.com/questions/34068684/call-another-fragment-page-when-clicking-google-map-infowindow?rq=1](http://stackoverflow.com/questions/34068684 /呼叫另一​​個片段的頁面時單擊-谷歌地圖-信息窗口?RQ = 1) –

回答

相關問題