2017-10-20 7 views
0

"MarkerOptions"は、データベースに保存されているマーカーを表示し、各マーカーには異なるフィールド(例:名前、電子メール、アドレスなど) 「.tittle」、「。snippet」を使用して、添付したいフィールドのいくつかを残しておきます。私は今、私が望むすべてのフィールドを表示するためにカスタム情報ウィンドウを使用する必要があることを知っています。これどうやってするの? マイコード:MySQLでカスタムINFOWINDOWを使用する

メイン:

ArrayList<HashMap<String, String>> location = null; 
    String url = "http://appserver.eco.mx/movil/getLanLong.php"; 
    try { 

     JSONArray data = new JSONArray(getHttpGet(url)); 
     location = new ArrayList<HashMap<String, String>>(); 
     HashMap<String, String> map; 

     for(int i = 0; i < data.length(); i++){ 
      JSONObject c = data.getJSONObject(i); 
      map = new HashMap<String, String>(); 
      map.put("id", c.getString("id")); 
      map.put("campo_latitud", c.getString("campo_latitud")); 
      map.put("campo_longitud", c.getString("campo_longitud")); 
      map.put("campo_categoria", c.getString("campo_categoria")); 
      map.put("campo_estado", c.getString("campo_estado")); 
      location.add(map); 
     } 
    } catch (JSONException e) { 
// TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    String campouno = ""; 
    if(!TextUtils.isEmpty(campouno)){ 
     double campo_latitud = Double.parseDouble(campouno); 
    } 
    //campo_latitud = Double.parseDouble(location.get(0).get("Latitude").toString()); 
    String campodos = ""; 
    if(!TextUtils.isEmpty(campodos)){ 
     double campo_longitud = Double.parseDouble(campodos); 
    } 

    for (int i = 0; i < location.size(); i++) { 
     if(!TextUtils.isEmpty(location.get(i).get("campo_latitud").toString())&&!TextUtils.isEmpty(location.get(i).get("campo_longitud").toString())) { 
      campo_latitud = Double.parseDouble(location.get(i).get("campo_latitud").toString()); 
      campo_longitud = Double.parseDouble(location.get(i).get("campo_longitud").toString()); 
     } 

String name = location.get(i).get("campo_categoria").toString(); 
     //String des = location.get(i).get("campo_descripcion").toString(); 

     if(location.get(i).get("campo_categoria").toString().equals("Obras publicas")){ 
      //System.out.println(location.get(i).get("campo_descripcion").toString()); 
      googleMap.addMarker(new MarkerOptions().position(new LatLng(campo_latitud, campo_longitud)).title(name).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_op))); 
     } 
    }} 

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:background="@android:color/white"> 

<TextView 
    android:id="@+id/info_window_nombre" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:paddingLeft="5dp" 
    android:text="Carlo Estrada Solano" /> 

<TextView 
    android:id="@+id/info_window_placas" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textStyle="bold" 
    android:textSize="20sp" 
    android:layout_below="@id/info_window_nombre" 
    android:paddingLeft="5dp" 
    android:text="Placas: SX5487" /> 

<TextView 
    android:id="@+id/info_window_estado" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/info_window_placas" 
    android:paddingLeft="5dp" 
    android:text="Estado: Activo" /> 

</LinearLayout> 

情報ウィンドウクラス:

public class AdaptadorInforWindow implements GoogleMap.InfoWindowAdapter { 

private static final String TAG = "CustomInfoWindowAdapter"; 
private LayoutInflater inflater; 

public AdaptadorInforWindow(LayoutInflater inflater){ 
    this.inflater = inflater; 
} 

@Override 
public View getInfoWindow(Marker marker) { 
    //Carga layout personalizado. 
    View v = inflater.inflate(R.layout.infowindow_layout, null); 
    String[] info = marker.getTitle().split("&"); 
    String url = marker.getSnippet(); 
    ((TextView)v.findViewById(R.id.info_window_nombre)).setText("Lina Cortés"); 
    ((TextView)v.findViewById(R.id.info_window_placas)).setText("Placas: SRX32"); 
    ((TextView)v.findViewById(R.id.info_window_estado)).setText("Estado: Activo"); 

    return v; 
} 

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

セットGoogleマップ:

myGoogleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(LayoutInflater.from(getActivity()))); 
+0

コードは正常です。何の問題? –

+0

私はそれがうまくいくのですが、私は必要なことをしません、私はcostum infowindowを使用したいですが、XMLで私は名前、アドレスなどを書く、私は名前とデータベースから来るアドレスを使用したいcostum infowindow @LalitSinghFauzdarに設定してください。 – Armando

答えて

0

私はちょうど私のアプリのCustomInfoWindowを作った。これを実現する方法は次のとおりです。

まず自分のマーカーに関連するすべてのデータにアクセスするには、データベースファイル内のメソッドを作成し、以下のようにコードがある:

Cursor getDetails(Marker marker){    
      db = getReadableDatabase();   
      String query = "select Name, Email, Address from YourTableName where Name = '"+marker.getTitle()+"';"; 
      return db.rawQuery(query,null); 
    } 

次に調整しますAdaptadorInforWindowのコードを次のように

YourDatabaseClass db = new YourDatabaseClass(context); 
    Cursor c = db.getDetails(); //Method you'll create in database class 
    ((TextView)v.findViewById(R.id.info_window_name)).setText(c.getString(0)); 
    ((TextView)v.findViewById(R.id.info_window_email)).setText(c.getString(1)); 
    ((TextView)v.findViewById(R.id.info_window_address)).setText(c.getString(2)); 
    c.close(); 

を参照してください。getStringが使用されています。私はあなたの名前、電子メール、アドレス値を保存するためにText/Stringデータ型を使用したと思います。データ型が変更された場合は、intの場合はc.getInt(index)などの適切なメソッドを使用する必要があります。

また、インデックス0,1,2はクエリ:名前電子メールアドレスと同じです。

この方法で、あなたのデータはCustomInfoWindowに表示されます。

同じことが私の中でうまく働いています。これがうまくいくことは間違いありません。

+0

@Armandoここに必要な答えがあります。 –

+0

これは、 "カーソルのgetDetails(マーカーマーカー){ db = getReadableDatabase(); を追加することを意味します。String query =" Name = '"+ marker.getTitle()+"'; "; return db.rawQuery(query、null); } "phpファイルでは? – Armando

+0

Argh !!!私はあなたがPHPを使用しているのを知らなかった... @アマンドしかし、あなたは現在のマーカーの詳細を取得するためにそれをカスタマイズすることはできますか? –

1

マーカーキーを使用してハッシュマップにマーカーを保存します。次に、ハッシュマップオブジェクトを使用してカスタム情報ウィンドウを使用して、他の情報を取得します。 マーカーをクリックすると、関連情報が表示されます。

か、このビデオは8時からワードで見ることができます。 Click Here

+0

私はどのようにマーカーキーを使用できますか?私はビデオを見ましたが、私はデータベースから値を取得しているところを見ませんでした – Armando

+1

https://stackoverflow.com/questions/16496715/how-to-know-which-marker-was-clicked-on-google- maps-v2-for-android –

+0

私が言ったことはできましたが、ちょっとした情報が私を大いに助けました! – Armando

関連する問題