2012-12-25 52 views
0

我一直在使用以下有關Google Maps API和Google Places API的教程。從谷歌顯示格式化的電話號碼將api放置在Android的提示對話框中?

http://www.androidhive.info/2012/08/android-working-with-google-places-and-maps-tutorial/

這是非常好的。我設法在地圖上顯示附近地點的標記,併爲不同類別顯示不同類型的標記。但是,當用戶點擊我的某個標記時,我還想在警告對話框中顯示該機構的電話號碼。爲此,我正在使用地點詳情搜索。

下面,我將提供我有問題的Map Activity代碼片段。

//Map Activity OnCreate() 
//Check for null nearPlaces 
if(nearPlaces.results!=null) 
{ 
    //Loop through all places 
    for(Place place:nearPlaces.results) 
    { 

     latitude=place.geometry.location.lat; 
     longitude=place.geometry.location.lng; 
     placeRef=place.reference; 

     new LoadDetails().execute(placeRef); 

     for(String s:place.types) 
     { 
      if(s.contains("restaurant")) 
      { 
       Drawable rMarker=this.getResources().getDrawable(R.drawable.restaurant); 

       //phone=lp.getPhone(); 
       iOverlay=new MyItemizedOverlay(rMarker,this); 

       //GeoPoint to place on map 
       gp=new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6)); 

       //Map overlay item 
       oItem=new OverlayItem(gp, place.name, place.vicinity+"\n"+"Rating: "+place.rating+" "+phone); 

       iOverlay.addOVerlay(oItem); 

       iOverlay.addOVerlay(oItem); 
       mapOverlays.add(iOverlay); 
      } 

我有一個變量的字符串電話,我想成爲企業的電話號碼。

這裏是LoadDetails類延伸的AsyncTask

private class LoadDetails extends AsyncTask<String, String, String> 
{ 
    @Override 
    protected void onPostExecute(String result) { 
       if(placeDetails!=null) 
       { 
        if(placeDetails.status.equals("OK")) 
        { 
         if(placeDetails.result!=null) 
         { 
          phone=placeDetails.result.formatted_phone_number; 
          Log.d("Phone #'s",phone); 
         } 
        } 
       } 



    } 

    @Override 
    protected String doInBackground(String... params) { 

     String reference=params[0]; 
     googlePlaces=new GooglePlaces(); 

     try{ 
      placeDetails=googlePlaces.getPlaceDetails(reference); 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
     return null; 
    } 


} 

在OnPostExecute,可變的手機設置和我的logcat顯示各個地方的電話號碼。但是,當我使用手機上的地圖活動並點擊餐館的標記時,可變電話始終爲空。我該如何解決?

任何幫助將是偉大的。

在此先感謝。

編輯:全MapActivity

公共類地圖擴展MapActivity {

MapView mapView; 
MapController mc; 
GeoPoint gp, fGP; 
List<Overlay> mapOverlays; 
OverlayItem oItem; 
MyItemizedOverlay iOverlay; 

String placeRef,phone,placePhone; 
double latitude,longitude; 
double user_lat,user_long; 
double friendLat,friendLong; 
String userName,friendName,userStatus,userPhoneNumber; 

//Nearest Places 
PlacesList nearPlaces; 

GooglePlaces googlePlaces; 

PlaceDetails placeDetails; 

@Override 
protected void onCreate(Bundle arg0) { 
    // TODO Auto-generated method stub 
    super.onCreate(arg0); 
    setContentView(R.layout.map); 


    mapView=(MapView) findViewById(R.id.mapView); 
    mapView.setBuiltInZoomControls(true); 

    mc=mapView.getController(); 

    Intent i=getIntent(); 

    if (i!=null) 
    { 
    //Receive User's Lat and Long 
    user_lat=i.getDoubleExtra("userLAT", 0); 
    user_long=i.getDoubleExtra("userLONG",0); 

    userPhoneNumber=i.getStringExtra("userNUMBER"); 

    //Get User's Status they posted 
    userStatus=i.getStringExtra("STATUS"); 
    //Get User's Name 
    userName=i.getStringExtra("userNAME"); 

    //Receive Friend's Lat and Long 

    friendLat=i.getDoubleExtra("friendLAT", 0); 
    friendLong=i.getDoubleExtra("friendLong",0); 


    //Obtain nearPlaces List from Main Activity 
    nearPlaces=(PlacesList) i.getSerializableExtra("userNearPlaces"); 


    friendName=i.getStringExtra("friendNAME"); 
    Log.d("Map", userName+ " " +user_lat + " " + user_long); 
    Log.d("Map2", friendName+ " " + friendLat + " " + friendLat); 
    gp=new GeoPoint((int)(user_lat*1E6),(int)(user_long*1E6)); 

    //Friend's GeoPoint 
    fGP=new GeoPoint((int)(friendLat*1E6),(int)(friendLong*1E6)); 

    } 

    mapOverlays=mapView.getOverlays(); 
    Drawable marker=this.getResources().getDrawable(R.drawable.you); 


    iOverlay=new MyItemizedOverlay(marker, this); 
    oItem=new OverlayItem(gp,"Your Location, "+userName,userStatus); 

    iOverlay.addOVerlay(oItem); 
    mapOverlays.add(iOverlay); 


    Drawable fMarker=this.getResources().getDrawable(R.drawable.smiley); 

    iOverlay=new MyItemizedOverlay(fMarker,this); 

    //Add friend marker to the map 
    oItem=new OverlayItem(fGP,friendName+"'s Location", "This is your friend"); 
    iOverlay.addOVerlay(oItem); 
    mapOverlays.add(iOverlay); 

    //Check for null nearPlaces 
    if(nearPlaces.results!=null) 
    { 
     //Loop through all places 
     for(Place place:nearPlaces.results) 
     { 

      latitude=place.geometry.location.lat; 
      longitude=place.geometry.location.lng; 
      placeRef=place.reference; 
      placePhone=place.phone; 

      new LoadDetails().execute(placeRef); 

      for(String s:place.types) 
      { 
       if(s.contains("restaurant")) 
       { 
        Drawable rMarker=this.getResources().getDrawable(R.drawable.restaurant); 

        //phone=lp.getPhone(); 
        iOverlay=new MyItemizedOverlay(rMarker,this); 

        //GeoPoint to place on map 
        gp=new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6)); 

        //Map overlay item 
        oItem=new OverlayItem(gp, place.name, place.vicinity+"\n"+"Rating: "+place.rating+" "+placePhone); 

        iOverlay.addOVerlay(oItem); 

        iOverlay.addOVerlay(oItem); 
        mapOverlays.add(iOverlay); 
       } 

       if((s.contains("cafe"))||place.name.contains("cafe")) 
       { 
        Drawable cafeMarker=this.getResources().getDrawable(R.drawable.cafe); 

        iOverlay=new MyItemizedOverlay(cafeMarker,this); 
        //GeoPoint to place on map 
        gp=new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6)); 

        //Map overlay item 
        oItem=new OverlayItem(gp, place.name, place.vicinity+"\n"+"Rating: "+place.rating); 

        iOverlay.addOVerlay(oItem); 

        iOverlay.addOVerlay(oItem); 
        mapOverlays.add(iOverlay); 

       } 

       else if((s.contains("liquor_store"))||place.name.contains("liquors")) 
       { 
        Drawable barMarker=this.getResources().getDrawable(R.drawable.bar); 

        iOverlay=new MyItemizedOverlay(barMarker,this); 
        //GeoPoint to place on map 
        gp=new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6)); 

        //Map overlay item 
        oItem=new OverlayItem(gp, place.name, place.vicinity+"\n"+"Rating: "+place.rating); 

        iOverlay.addOVerlay(oItem); 

        iOverlay.addOVerlay(oItem); 
        mapOverlays.add(iOverlay); 

       } 

       else if(s.contains("department_store")||s.contains("clothing_store")||s.contains("convenience_store")) 
       { 
        Drawable shopMarker=this.getResources().getDrawable(R.drawable.mall); 

        iOverlay=new MyItemizedOverlay(shopMarker,this); 
        //GeoPoint to place on map 
        gp=new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6)); 

        //Map overlay item 
        oItem=new OverlayItem(gp, place.name, place.vicinity+"\n"+"Rating: "+place.rating); 

        iOverlay.addOVerlay(oItem); 

        iOverlay.addOVerlay(oItem); 
        mapOverlays.add(iOverlay); 

       } 

       else if(s.contains("hospital")) 
       { 
        Drawable hMarker=this.getResources().getDrawable(R.drawable.hospital); 

        iOverlay=new MyItemizedOverlay(hMarker,this); 
        //GeoPoint to place on map 
        gp=new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6)); 

        //Map overlay item 
        oItem=new OverlayItem(gp, place.name, place.vicinity+"\n"+"Rating: "+place.rating); 

        iOverlay.addOVerlay(oItem); 

        iOverlay.addOVerlay(oItem); 
        mapOverlays.add(iOverlay); 

       } 

       else if(s.contains("movie_theater")) 
       { 
        Drawable movieMarker=this.getResources().getDrawable(R.drawable.movierental); 

        iOverlay=new MyItemizedOverlay(movieMarker,this); 
        //GeoPoint to place on map 
        gp=new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6)); 

        //Map overlay item 
        oItem=new OverlayItem(gp, place.name, place.vicinity+"\n"+"Rating: "+place.rating); 

        iOverlay.addOVerlay(oItem); 

        iOverlay.addOVerlay(oItem); 
        mapOverlays.add(iOverlay); 

       } 

       else if(s.contains("pharmacy")) 
       { 
        Drawable pMarker=this.getResources().getDrawable(R.drawable.pharmacy); 

        iOverlay=new MyItemizedOverlay(pMarker,this); 
        //GeoPoint to place on map 
        gp=new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6)); 

        //Map overlay item 
        oItem=new OverlayItem(gp, place.name, place.vicinity+"\n"+"Rating: "+place.rating); 

        iOverlay.addOVerlay(oItem); 

        iOverlay.addOVerlay(oItem); 
        mapOverlays.add(iOverlay); 

       } 
       else if(s.contains("hair_care")) 
       { 
        Drawable bMarker=this.getResources().getDrawable(R.drawable.barber); 

        iOverlay=new MyItemizedOverlay(bMarker,this); 
        //GeoPoint to place on map 
        gp=new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6)); 

        //Map overlay item 
        oItem=new OverlayItem(gp, place.name, place.vicinity+"\n"+"Rating: "+place.rating); 

        iOverlay.addOVerlay(oItem); 

        iOverlay.addOVerlay(oItem); 
        mapOverlays.add(iOverlay); 

       } 

      } 


     } 
    } 

    mapView.getController(); 

    mc.animateTo(gp); 
    mc.setZoom(15); 
    mc.setCenter(gp); 

    mapView.invalidate(); 
} 



@Override 
protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
} 

private class LoadDetails extends AsyncTask<String, String, String> 
{ 
    @Override 
    protected void onPostExecute(String result) { 
     placePhone=result; 

        //My Logcat shows phone numbers being assigned to placePhone 
     Log.d("placePhone",placePhone); 

    } 

    @Override 
    protected String doInBackground(String... params) { 

     String reference=params[0]; 
     googlePlaces=new GooglePlaces(); 

     try{ 
      placeDetails=googlePlaces.getPlaceDetails(reference); 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
     if(placeDetails!=null) 
     { 
      if(placeDetails.status.equals("OK")) 
      { 
       if(placeDetails.result!=null) 
       { 
        phone=placeDetails.result.formatted_phone_number; 

       } 
      } 
     } 
     return phone; 
    } 


} 

private class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> 
{ 
    private ArrayList<OverlayItem> mOverlays=new ArrayList<OverlayItem>(); 
    Context mContext; 

    public MyItemizedOverlay(Drawable marker) { 
     super(boundCenterBottom(marker)); 
    } 

    public MyItemizedOverlay(Drawable marker,Context context) { 
     super(boundCenterBottom(marker)); 
     mContext=context; 
    } 

    @Override 
    protected OverlayItem createItem(int i) { 
     return mOverlays.get(i); 
    } 

    @Override 
    public int size() { 
     return mOverlays.size(); 
    } 

    public void addOVerlay(OverlayItem overlay) 
    { 
     mOverlays.add(overlay); 
     populate(); 
    } 


    @Override 
    public boolean onTap(int i) { 

     OverlayItem item= mOverlays.get(i); 
     AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); 
     dialog.setTitle(item.getTitle()); 
     dialog.setMessage(item.getSnippet()); 


     if(item.getTitle().contains("Location")) 
     { 
     dialog.setNegativeButton("Send SMS", new OnClickListener() { 

      public void onClick(DialogInterface dialog, int which) { 
       Intent smsIntent=new Intent(Intent.ACTION_SENDTO); 
       smsIntent.addCategory(Intent.CATEGORY_DEFAULT); 
       smsIntent.setType("vnd.android-dir/mms-sms"); 
       smsIntent.setData(Uri.parse("sms:"+userPhoneNumber)); 
       startActivity(smsIntent); 

      } 
     }); 

     dialog.setPositiveButton("Call", new OnClickListener() { 

      public void onClick(DialogInterface dialog, int which) { 
       String uri="tel:"+userPhoneNumber.trim(); 
       Intent callIntent=new Intent(Intent.ACTION_CALL); 
       callIntent.setData(Uri.parse(uri)); 
       startActivity(callIntent); 

      } 
     }); 
     } 
     dialog.show(); 

     return true; 
    } 


} 

}

回答

0
oItem=new OverlayItem(gp, place.name, place.vicinity+"\n"+"Rating: "+place.rating+" "+ place.formatted_phone_number); 

試試這個place.formatted_phone_number

+0

感謝您的答覆,但我的地方類沒有任何一個數據成員稱爲手機,所以我創建了一個。 我在placeRef後發了一個名爲placePhone = place.phone的String變量。 After phone = placeDetails.result.formatted_phone_number;在我的onPostExecute中,我有以下任務placePhone = phone; 然後在我的地圖活動中,我有 oItem = new OverlayItem(gp,place.name,place.vicinity +「\ n」+「評分:」+ place.rating +「」+ PlacePhone); 但仍然爲空。我有一種感覺,這是一個簡單的修復,但我似乎無法得到它 –

+0

你可以粘貼你的整個MapActivity? –

+0

是的,當然,請查看我上面的修改。我會警告你這是瘋狂的冗長。 –