2014-05-20 26 views
0

我在Android設備上使用Google地圖,我從markes獲取信息,我能夠獲取標記標題和製造商片段,但無法獲取電話號碼信息,請幫助我,謝謝。提前。從谷歌地圖標記獲取電話號碼

package com.avion.mapdemo; 

公共類MainActivity擴展活動實現OnInfoWindowClickListener {

// Google Map 
private GoogleMap googleMap; 
MarkerOptions markerOptions; 
LatLng latLng; 
GPSTracker gps; 
Address adrs; 

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

    try { 
     // Loading map 

     initilizeMap(); 

     // my location... 
     googleMap.setMyLocationEnabled(true); 

     // room setting from 2(min) to 21 (max).. 
     googleMap.animateCamera(CameraUpdateFactory.zoomTo(10.0f)); 

     // get my location address...... 

     myLocation(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    // marker information tab clicked........... 

    googleMap.setOnInfoWindowClickListener(this); 

} 

// method my location 
private void myLocation() { 

    // TODO Auto-generated method stub 

    // for logitude and latitude.......... 

    gps = new GPSTracker(MainActivity.this); 

    // check if GPS enabled 
    if (gps.canGetLocation()) { 

     double latitude = gps.getLatitude(); 
     double longitude = gps.getLongitude(); 

     //double latitude = 40.71958; 
     // double longitude = -74.09595; 


     // Toast.makeText(getApplicationContext(),latitude 
     // +"--"+longitude,Toast.LENGTH_SHORT).show(); 

     // for address.......... 
     Geocoder geocoder; 
     List<Address> addresses = null; 
     geocoder = new Geocoder(this, Locale.getDefault()); 
     try { 
      addresses = geocoder.getFromLocation(latitude, longitude, 1); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     String address = addresses.get(0).getAddressLine(0); 
     String city = addresses.get(0).getAddressLine(1); 
     String country = addresses.get(0).getAddressLine(2); 

     Toast.makeText(getApplicationContext(), 
       address + "" + city + "" + country, Toast.LENGTH_LONG) 
       .show(); 

     Log.e("strite", address); 
     Log.e("city", city); 
     Log.e("country", country); 
     // search string..... 
     // String addr="Hotel "+address 
     // +" "+city+" "+country; 

     String addr = "Bar" + " " + city + " " + country; 

     // call async task for load bars..... 

     new GeocoderTask().execute(addr); 

    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

/** 
* function to load map. If map is not created it will create it for you 
* */ 
private void initilizeMap() { 
    if (googleMap == null) { 
     googleMap = ((MapFragment) getFragmentManager().findFragmentById(
       R.id.map)).getMap(); 

     // check if map is created successfully or not 
     if (googleMap == null) { 
      Toast.makeText(getApplicationContext(), 
        "Sorry! unable to create maps", Toast.LENGTH_SHORT) 
        .show(); 
     } 
    } 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    initilizeMap(); 
} 

// An AsyncTask class for accessing the GeoCoding Web Service 
private class GeocoderTask extends AsyncTask<String, Void, List<Address>> { 

    @Override 
    protected List<Address> doInBackground(String... locationName) { 
     // Creating an instance of Geocoder class 
     // Geocoder transforming a street address or other description of a 
     // location 
     // into a (latitude, longitude) coordinate. 
     Geocoder geocoder = new Geocoder(getBaseContext()); 
     List<Address> addresses = null; 

     try { 
      // Getting a maximum of 10 Address that matches the input text 
      addresses = geocoder.getFromLocationName(locationName[0], 10); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return addresses; 
    } 

    @Override 
    protected void onPostExecute(List<Address> addresses) { 

     if (addresses == null || addresses.size() == 0) { 
      Toast.makeText(getBaseContext(), "No Bar found", 
        Toast.LENGTH_SHORT).show(); 
     } 

     // Clears all the existing markers on the map 
     googleMap.clear(); 

     // Adding Markers on Google Map for each matching address 
     for (int i = 0; i < addresses.size(); i++) { 

      // address Strings describing a location 
      adrs = (Address) addresses.get(i); 

      // Creating an instance of GeoPoint, to display in Google Map 
      // latLng class representing a pair of latitude and longitude 
      // coordinates, 

      latLng = new LatLng(adrs.getLatitude(), adrs.getLongitude()); 

      // latLng = new LatLng(40.71958,-74.09595); 

      String addressText = String.format("%s, %s", adrs 
        .getMaxAddressLineIndex() > 0 ? adrs.getAddressLine(0) 
        : "", adrs.getCountryName()); 

      // markerOptions to add property to marker 

      markerOptions = new MarkerOptions(); 
      markerOptions.position(latLng); 
      markerOptions.title(adrs.getAddressLine(0)); 
      markerOptions.snippet(adrs.getAddressLine(1) + ", " 
        + adrs.getAddressLine(2) + ", " 
        + adrs.getPhone()); 

      /* 
      * getAddressLine(0) location name . 
      * getAddressLine(1) local address . 
      * getAddressLine(2) city,state . 
      * getAddressLine(3) country . 
      */ 
      googleMap.addMarker(markerOptions); 

      // Locate the first location 
      if (i == 0) 
       googleMap.animateCamera(CameraUpdateFactory 
         .newLatLng(latLng)); 
     } 
     // end of loop..... 
    } 
} 

// on marker info tab click.. 
@Override 
public void onInfoWindowClick(Marker marker) { 

    Toast.makeText(this, marker.getTitle() + "--" + marker.getSnippet(), 
      Toast.LENGTH_LONG).show(); 

} 

}

+0

告訴我你的代碼是如何獲取信息? –

+0

在最後,我要添加電話號碼 – user3395433

+0

任何幫助完整的代碼或網站鏈接??? – user3395433

回答

1

你應該splitSnippetMarker文本,並提取所有的細節。

見下文

public void onInfoWindowClick(Marker marker) { 

String[] str2 = marker.getSnippet().split(","); 
String Addressline1=str2[0]; //Addressline 1 
String Addressline2=str2[1]; //Addressline 2 
String phone=str2[2]; //Phone 

Toast.makeText(this, marker.getTitle() + "--" + marker.getSnippet()+"-- "+phone, 
     Toast.LENGTH_LONG).show(); 
} 
+0

沒有朋友,它只是顯示一些地址的部分,是他們的任何inbuild函數? – user3395433

+0

@ user3395433這是因爲你必須用'用一個空格'連接你的字符串。明白我在談論什麼?仔細檢查你的代碼。你必須contcat使用單,(逗號)'沒有空格/ –

+0

沒有frd,我也試過,問題是我的連接字符串也dnt有手機號碼,所以他們沒有處理concatnation – user3395433