我已经做谷歌地图v2演示。我有许多纬度和经度可以在地图上作为标记显示,我已经成功完成了这项工作。当谷歌地图标记被点击时,显示图像和文字android
但是,现在我想要在点击标记时向他们显示图像,这也差不多完成了,但是我的问题是所有标记都是单张图像。
代码
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();
}
}
}
我只是在没有代码编辑器的情况下输入它,但这是跟踪标记的另一种方式。 – danny117 2015-03-13 19:53:51