【问题标题】:how can get the information for place on google map?如何在谷歌地图上获取地点信息?
【发布时间】:2017-12-11 09:32:04
【问题描述】:

我想在搜索餐厅或某个地方时使用来自 Google 的信息

我尝试在点击 Google 地图中的标记时启动一项活动

所以使用 onMarkerClick 但它只有我需要的名称、地址、地点 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;
}

如何获取该位置的电话号码、评级和照片等其他信息?

【问题讨论】:

  • 我的问题不正确,我可以制作信息窗口,但我无法获取评分点、电话号码等所有信息
  • “我无法获取所有信息”从哪里来?用适当的问题编辑您的问题。
  • 如果您有解决方案请告诉我,请编辑我的问题

标签: android google-maps marker


【解决方案1】:

您想在标记信息窗口中显示更多数据,因此您需要在创建标记时将一些数据放入标记中。您应该做一些新的事情: 1.首先创建一个hash map来放值。

 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);

当你在谷歌地图信息窗口设置listner时,它返回标记对象getmarker.getsn-p,它返回hashmap类型字符串,根据此代码将其转换为单键值:

 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();

这将返回标记“用户名”数据,在需要的地方使用。

【讨论】:

  • 感谢您的回答,但我的问题不正确,所以它对我不起作用
【解决方案2】:

试试这个自定义标记的完整示例。

地图活动:

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>

【讨论】:

  • 感谢您的回答,但我的问题不正确,所以它对我不起作用
猜你喜欢
  • 2016-12-06
  • 1970-01-01
  • 2011-12-04
  • 2018-09-05
  • 2015-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
相关资源
最近更新 更多