2017-12-11 12 views
-3

私はGoogleマップでマーカーがどのようにGoogleマップ上の場所の情報を取得できますか?

をクリックされたとき、私はそうonMarkerClickを使用しますが、それは名前だけを持って、アドレス活動を開始作ってみました、私はレストランやいくつかの場所

を検索する際にGoogleからの情報を使用したいです、場所ID私は必要なもの

@Override 
public boolean onMarkerClick(Marker marker) { 
    Id = marker.getId(); 

    String Title = marker.getTitle(); 
    String Address = marker.getSnippet(); 

    switch(databaseCheck){ 
     case 1: 
      Intent intent = new Intent(mActivity, PopupGrid.class); 
      intent.putExtra("Title" , Title); 
      intent.putExtra("Address", Address); 
      getActivity().startActivity(intent); 
      break; 
     case 2: 
      Intent intent2 = new Intent(mActivity, Nodatabase.class); 
      intent2.putExtra("Title" , Title); 
      intent2.putExtra("Address", Address); 
      getActivity().startActivity(intent2); 
      break; 
    } 

    return true; 
} 

どのように他の情報は、電話番号、評価、その場所の写真ですか?

+1

[GoogleがV2をマップでカスタム情報ウィンドウを作成]の可能複製(https://stackoverflow.com/questions/17410282/createであなたが望むすべてのオプションを宣言-a-custom-info-window-in-google-maps-v2) – ADM

+0

私の質問は正しくありませんでした。私は情報ウィンドウを作ることができますが、評価点、電話番号などのようなすべての情報を得ることはできません – Jshin

+0

"どこ?適切な問題であなたの質問を編集してください。 – ADM

答えて

0

マーカー情報ウィンドウにさらにデータを表示したいので、作成時にマーカーにデータを入れる必要があります。新しいものを作成する必要があります: 1.まずハッシュマップを作成して値を入れます。

HashMap<String, String> map = new HashMap<>(); 
     map.put("userId", userId); 
     map.put("userName", name); 
     if(profileImage.equalsIgnoreCase("")) 
     map.put("imageUrl", null); 
     else 
      map.put("imageUrl", profileImage); 
     map.put("primaryInstrumentName", primary_Instrument_name); 

     googleMap.addMarker(new MarkerOptions() 
       .position(new LatLng(Double.valueOf(latitude), Double.valueOf(lonnitude))).snippet(String.valueOf(map)) 
       .icon(R.drawable.marker); 

uは、マーカーオブジェクトのGoogleマップの情報ウィンドウにそのリターンをLISTNERを設定する際にハッシュマップタイプの文字列を返すmarker.getsnippetは、このコードに応じて、単一のキー値でそれを変換します:

private Map<String, String> getMarkerDetails(Marker marker) { 
     String name2 = marker.getSnippet(); 
     System.out.println("name "+name2); 

     name2 = name2.substring(1, name2.length() - 1); 
     String[] keyValuePairs = name2.split(","); 
     Map<String, String> map = new HashMap<>(); 

     for (String pair : keyValuePairs) { 
      String[] entry = pair.split("="); 
      System.out.println("name map is "+entry[0]+" "+entry[1]); 
      map.put(entry[0].trim(), entry[1].trim()); 
     } 
     return map; 
    } 

この

getMarkerDetails(marker).get("userName").toString(); 

これは、マーカー「ユーザー名」データに戻り、必要な場合はいつでも使用して、文字列ハッシュマップからキー値を返すのに役立ちます。

+0

あなたの答えをありがとうございますが、私の質問は正しいので、それは私のためにdosent仕事 – Jshin

0

カスタムマーカーのこの完全な例をお試しください。

地図アクティビティ:

public void onMapReady(GoogleMap googleMap) { 
mMap.clear(); 
LinearLayout tv2; 
tv2 = (LinearLayout)MapsActivity.this.getLayoutInflater().inflate(R.layout.marker_dialog, 
null, false); 
tv2.measure(View.MeasureSpec.makeMeasureSpec(0, 
View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, 
View.MeasureSpec.UNSPECIFIED)); 
tv2.layout(0, 0, tv2.getMeasuredWidth(), tv2.getMeasuredHeight()); 
tv2.setDrawingCacheEnabled(true); 
tv2.buildDrawingCache(); 
Bitmap bm2 = tv2.getDrawingCache(); 
icon2 = BitmapDescriptorFactory.fromBitmap(bm2); 
BitmapDescriptorFactory.fromResource(R.drawable.ic_markericon); 

HashMap<Marker, JSONObject> stopsMarkersInfo = new HashMap<>(); 
JSONObject ObjectForMarker; 
for (int i = 0; i < YourData.length(); i++) 
{//YourData is JSONArray 
    LatLng coordinate = new LatLng(Latitude, Longitude); 
    ObjectForMarker = YourData.getJSONObject(i); 
    MarkerOptions markerOptions = new 
    MarkerOptions().position(coordinate).icon(Youricon); 
    Marker marker = mMap.addMarker(markerOptions); 
    stopsMarkersInfo.put(marker, ObjectForMarker); 
} 
googleMap.setInfoWindowAdapter(new StopsInfoWindow(stopsMarkersInfo, getApplicationContext())); 
    } 

カスタムアダプタクラス:

import android.content.Context; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.widget.TextView; 
    import com.google.android.gms.maps.GoogleMap; 
    import com.google.android.gms.maps.model.Marker; 
    import org.json.JSONException; 
    import org.json.JSONObject; 
    import java.util.HashMap; 
    public class StopsInfoWindow implements GoogleMap.InfoWindowAdapter { 

private HashMap<Marker, JSONObject> stopsMarkersInfo; 
private View view; 
Context context; 

public StopsInfoWindow(HashMap<Marker, JSONObject> stopsMarkersInfo, Context context) { 
    this.stopsMarkersInfo = stopsMarkersInfo; 
    this.context = context; 
} 


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

@Override 
public View getInfoWindow(final Marker marker) { 
    JSONObject stop = stopsMarkersInfo.get(marker); 
    if (stop != null) { 
     LayoutInflater inflater = (LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     view = inflater.inflate(R.layout.item_stop_marker_info, null); 

     TextView iD = (TextView) view.findViewById(R.id.iD); 
     TextView Amount = (TextView) view.findViewById(R.id.Amount); 
     TextView Date = (TextView) view.findViewById(R.id.Date); 



     for (int i = 0; i < stopsMarkersInfo.size(); i++) { 

     } 

     try { 
      iD.setText(stop.getString("iD")); 
      Amount.setText(stop.getString("Amount")); 
      Date.setText(stop.getString("Date")); 


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


    } 
    return view; 
    } 
    } 

marker_dialog.xml:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myMarker" 
    android:layout_width="100dp" 
    android:layout_height="wrap_content" 
    android:background="@android:color/transparent" 
    android:orientation="vertical"> 
    <ImageView 
    android:layout_width="50dp" 
    android:layout_height="60dp" 
    android:layout_gravity="center_horizontal" 
    android:src="@drawable/ic_markericon" /> 
    </LinearLayout> 

item_stop_marker_info.xml: はあなたのマーカー

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/roundlayout"> 

    <TextView 
    android:id="@+id/iD" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_toRightOf="@+id/MName" 
    android:text="iD" 
    android:textColor="@color/background" 
    android:textSize="13dp" /> 

<TextView 
    android:layout_marginRight="10dp" 
    android:id="@+id/Amount" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Amount" 
    android:textColor="@color/background" 
    android:textSize="13dp" /> 
    </RelativeLayout> 
+0

あなたの答えをありがとう、私の質問は、 – Jshin

関連する問題