在 android 中单击 google 地图标记时显示图像和文本
display image and text when google map marker is clicked in android
我制作了 google 地图 v2 演示。我有许多纬度和经度要在地图上显示为标记,我已经成功完成了。
但是,现在我想在单击标记时向他们显示图像,这也差不多完成了,但我的问题是所有标记我得到的是单张图像。
代码
package com.amar.travelonwards;
import java.io.IOException;
import java.net.URL;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.Html;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.amar.travelonwards.utility.ImageLoader;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class Multi_Map_Marker extends Activity {
// Google Map
private GoogleMap googleMap;
int[] img = { R.drawable.ic_launcher, R.drawable.aro, R.drawable.back };
CameraPosition cameraPosition;
ImageLoader imageLoader;
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.multi_map_marker);
try {
// Loading map
imageLoader = new ImageLoader(Multi_Map_Marker.this);
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
/**
* function to load map. If map is not created it will create it for you
*
* @throws IOException
* */
@SuppressLint("NewApi")
private void initilizeMap() throws IOException {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map_multi)).getMap();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
// create marker
for (int i = 0; i < HotelListActivity.mNewsFeeder.latitude_list
.size(); i++) {
String latitude = HotelListActivity.mNewsFeeder.latitude_list
.get(i);
String longitude = HotelListActivity.mNewsFeeder.longitude_list
.get(i);
String name = HotelListActivity.mNewsFeeder.hotel_name_list
.get(i);
String hotel_images = "http://images.travelnow.com"
+ HotelListActivity.mNewsFeeder.hotel_image_list.get(i);
System.out.println("MYIMAGES " + hotel_images);
URL url = new URL(hotel_images);
Bitmap image = BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
MarkerOptions marker = new MarkerOptions()
.position(
new LatLng(Double.valueOf(latitude), Double
.valueOf(longitude))).title(name)
.snippet("Bed " + Integer.toString(i))
/* .icon(BitmapDescriptorFactory.fromBitmap(image)) */;
try {
googleMap.addMarker(marker);
cameraPosition = new CameraPosition.Builder()
.target(new LatLng(Double.valueOf(latitude), Double
.valueOf(longitude))).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
try {
googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View myContentsView = getLayoutInflater().inflate(
R.layout.custom_info_contents, null);
TextView tvTitle = ((TextView) myContentsView
.findViewById(R.id.title));
ImageView image_view = ((ImageView) myContentsView
.findViewById(R.id.image_view));
tvTitle.setText(marker.getTitle());
for (i = 0; i < HotelListActivity.mNewsFeeder.latitude_list
.size(); i++) {
try
{
i = Integer.parseInt(marker.getSnippet());
}
catch (java.lang.NumberFormatException e)
{
i = 0;
}
String hotel_images = HotelListActivity.mNewsFeeder.hotel_image_list
.get(i);
System.out.println("MYIMAGES " + hotel_images);
imageLoader.DisplayImage(hotel_images, image_view);
}
TextView tvSnippet = ((TextView) myContentsView
.findViewById(R.id.snippet));
return myContentsView;
}
});
} catch (Exception e) {
// TODO: handle exception
}
// adding marker
// 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();
try {
initilizeMap();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
方法getInfoContents(Marker marker)
会给你点击标记作为参数。
与其遍历 mNewsFeeder
列表中的所有项目并在图像视图中加载每张图像,不如在列表中找到与给定标记相对应的项目(索引)并加载其图像。
由于您在创建标记时没有设置 snippet
值,实现这一点的最简单方法可能是在创建标记时设置索引,然后使用该索引获取正确的图像。
MarkerOptions marker = new MarkerOptions().position(
new LatLng(Double.valueOf(latitude), Double.valueOf(longitude)))
.title(name)
.snippet(Integer.toString(i));
而不是循环
for (int i = 0; i < HotelListActivity.mNewsFeeder.latitude_list.size(); i++) {
使用
try
{
i = Integer.parseInt(marker.getsnippet());
}
catch (java.lang.NumberFormatException e)
{
i = 0;
}
final String hotel_images = HotelListActivity.mNewsFeeder.hotel_image_list.get(i);
imageLoader.DisplayImage(hotel_images, image_view);
另一种方式。当您将标记添加到地图时,获取分配给它的 id。
marker = mMap.add(markerOptions);
现在保存标记 id
x.markerId = marker.getId();
你不能扩展 markeroptions 因为它是最终的所以制作一个 class 有一个 markerOptions 和 id。
Class X {
MarkerOptions markerOptions;
string markerId;
string pathToImage;
int imageId;
string myCustomMarkerProperty;
}
将 x 保存到集合中
blah...
单击标记时,使用它的 ID 在集合中查找。
marker.getId()
我制作了 google 地图 v2 演示。我有许多纬度和经度要在地图上显示为标记,我已经成功完成了。
但是,现在我想在单击标记时向他们显示图像,这也差不多完成了,但我的问题是所有标记我得到的是单张图像。
代码
package com.amar.travelonwards;
import java.io.IOException;
import java.net.URL;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.Html;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.amar.travelonwards.utility.ImageLoader;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class Multi_Map_Marker extends Activity {
// Google Map
private GoogleMap googleMap;
int[] img = { R.drawable.ic_launcher, R.drawable.aro, R.drawable.back };
CameraPosition cameraPosition;
ImageLoader imageLoader;
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.multi_map_marker);
try {
// Loading map
imageLoader = new ImageLoader(Multi_Map_Marker.this);
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
/**
* function to load map. If map is not created it will create it for you
*
* @throws IOException
* */
@SuppressLint("NewApi")
private void initilizeMap() throws IOException {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map_multi)).getMap();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
// create marker
for (int i = 0; i < HotelListActivity.mNewsFeeder.latitude_list
.size(); i++) {
String latitude = HotelListActivity.mNewsFeeder.latitude_list
.get(i);
String longitude = HotelListActivity.mNewsFeeder.longitude_list
.get(i);
String name = HotelListActivity.mNewsFeeder.hotel_name_list
.get(i);
String hotel_images = "http://images.travelnow.com"
+ HotelListActivity.mNewsFeeder.hotel_image_list.get(i);
System.out.println("MYIMAGES " + hotel_images);
URL url = new URL(hotel_images);
Bitmap image = BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
MarkerOptions marker = new MarkerOptions()
.position(
new LatLng(Double.valueOf(latitude), Double
.valueOf(longitude))).title(name)
.snippet("Bed " + Integer.toString(i))
/* .icon(BitmapDescriptorFactory.fromBitmap(image)) */;
try {
googleMap.addMarker(marker);
cameraPosition = new CameraPosition.Builder()
.target(new LatLng(Double.valueOf(latitude), Double
.valueOf(longitude))).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
try {
googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View myContentsView = getLayoutInflater().inflate(
R.layout.custom_info_contents, null);
TextView tvTitle = ((TextView) myContentsView
.findViewById(R.id.title));
ImageView image_view = ((ImageView) myContentsView
.findViewById(R.id.image_view));
tvTitle.setText(marker.getTitle());
for (i = 0; i < HotelListActivity.mNewsFeeder.latitude_list
.size(); i++) {
try
{
i = Integer.parseInt(marker.getSnippet());
}
catch (java.lang.NumberFormatException e)
{
i = 0;
}
String hotel_images = HotelListActivity.mNewsFeeder.hotel_image_list
.get(i);
System.out.println("MYIMAGES " + hotel_images);
imageLoader.DisplayImage(hotel_images, image_view);
}
TextView tvSnippet = ((TextView) myContentsView
.findViewById(R.id.snippet));
return myContentsView;
}
});
} catch (Exception e) {
// TODO: handle exception
}
// adding marker
// 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();
try {
initilizeMap();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
方法getInfoContents(Marker marker)
会给你点击标记作为参数。
与其遍历 mNewsFeeder
列表中的所有项目并在图像视图中加载每张图像,不如在列表中找到与给定标记相对应的项目(索引)并加载其图像。
由于您在创建标记时没有设置 snippet
值,实现这一点的最简单方法可能是在创建标记时设置索引,然后使用该索引获取正确的图像。
MarkerOptions marker = new MarkerOptions().position(
new LatLng(Double.valueOf(latitude), Double.valueOf(longitude)))
.title(name)
.snippet(Integer.toString(i));
而不是循环
for (int i = 0; i < HotelListActivity.mNewsFeeder.latitude_list.size(); i++) {
使用
try
{
i = Integer.parseInt(marker.getsnippet());
}
catch (java.lang.NumberFormatException e)
{
i = 0;
}
final String hotel_images = HotelListActivity.mNewsFeeder.hotel_image_list.get(i);
imageLoader.DisplayImage(hotel_images, image_view);
另一种方式。当您将标记添加到地图时,获取分配给它的 id。
marker = mMap.add(markerOptions);
现在保存标记 id
x.markerId = marker.getId();
你不能扩展 markeroptions 因为它是最终的所以制作一个 class 有一个 markerOptions 和 id。
Class X {
MarkerOptions markerOptions;
string markerId;
string pathToImage;
int imageId;
string myCustomMarkerProperty;
}
将 x 保存到集合中
blah...
单击标记时,使用它的 ID 在集合中查找。
marker.getId()