我已经制作了谷歌地图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();
}
}
}
答案 0 :(得分:0)
方法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);
答案 1 :(得分:0)
另一种方式。将标记添加到地图时,请获取分配给它的ID。
marker = mMap.add(markerOptions);
现在保存标记ID
x.markerId = marker.getId();
您无法扩展markeroptions,因为它是最终的,因此请创建一个具有markerOptions和id的类。
Class X {
MarkerOptions markerOptions;
string markerId;
string pathToImage;
int imageId;
string myCustomMarkerProperty;
}
将x保存到集合中
blah...
单击标记时,使用它的id在集合中查找。
marker.getId()