Java 使用图像onclick marker OpenStreetMap显示标记详细信息
我正在使用OpenStreetMap创建一个简单的地图应用程序。我已经在地图视图中打开了地图,并在地图上添加了标记。这一切都很好现在,当用户单击标记时,会弹出一个描述框,其中包含该位置的名称、描述和图像视图。 主要活动:Java 使用图像onclick marker OpenStreetMap显示标记详细信息,java,android,openstreetmap,marker,osmdroid,Java,Android,Openstreetmap,Marker,Osmdroid,我正在使用OpenStreetMap创建一个简单的地图应用程序。我已经在地图视图中打开了地图,并在地图上添加了标记。这一切都很好现在,当用户单击标记时,会弹出一个描述框,其中包含该位置的名称、描述和图像视图。 主要活动: public class MainActivity extends Activity { MyItemizedOverlay myItemizedOverlay = null; @Override protected void onCreate(B
public class MainActivity extends Activity {
MyItemizedOverlay myItemizedOverlay = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
Drawable marker=getResources().getDrawable(R.drawable.pin_for_map);
int markerWidth = marker.getIntrinsicWidth();
int markerHeight = marker.getIntrinsicHeight();
marker.setBounds(0, markerHeight, markerWidth, 0);
ResourceProxy resourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
myItemizedOverlay = new MyItemizedOverlay(marker, resourceProxy);
mapView.getOverlays().add(myItemizedOverlay);
GeoPoint myPoint1 = new GeoPoint(0*1000000, 0*1000000);
myItemizedOverlay.addItem(myPoint1, "myPoint1", "myPoint1");
GeoPoint myPoint2 = new GeoPoint(50*1000000, 50*1000000);
myItemizedOverlay.addItem(myPoint2, "myPoint2", "myPoint2");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="5dp"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<org.osmdroid.views.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"/>
</RelativeLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.openstreetmaptutorial"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.openstreetmaptutorial.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MyItemizeOverlay类:
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();
public MyItemizedOverlay(Drawable pDefaultMarker,
ResourceProxy pResourceProxy) {
super(pDefaultMarker, pResourceProxy);
// TODO Auto-generated constructor stub
}
public void addItem(GeoPoint p, String title, String snippet){
OverlayItem newItem = new OverlayItem(title, snippet, p);
overlayItemList.add(newItem);
populate();
}
@Override
public boolean onSnapToItem(int arg0, int arg1, Point arg2, IMapView arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
protected OverlayItem createItem(int arg0) {
// TODO Auto-generated method stub
return overlayItemList.get(arg0);
}
@Override
public int size() {
// TODO Auto-generated method stub
return overlayItemList.size();
}
}
公共类MyItemizeOverlay扩展了ItemizeOverlay{
private ArrayList overlayItemList=新建ArrayList();
公共MyItemizedOverlay(可抽出式pDefaultMarker,
资源代理(资源代理){
super(pDefaultMarker,尿前造影);
//TODO自动生成的构造函数存根
}
公共void附加项(地质点p、字符串标题、字符串片段){
OverlayItem newItem=新的OverlayItem(标题、片段、p);
overlayItemList.add(newItem);
填充();
}
@凌驾
NaptoItem上的公共布尔值(int arg0、int arg1、点arg2、IMapView arg3){
//TODO自动生成的方法存根
返回false;
}
@凌驾
受保护的OverlayItem createItem(int arg0){
//TODO自动生成的方法存根
返回overlayItemList.get(arg0);
}
@凌驾
公共整数大小(){
//TODO自动生成的方法存根
返回overlayItemList.size();
}
}
活动\u main.xml:
public class MainActivity extends Activity {
MyItemizedOverlay myItemizedOverlay = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
Drawable marker=getResources().getDrawable(R.drawable.pin_for_map);
int markerWidth = marker.getIntrinsicWidth();
int markerHeight = marker.getIntrinsicHeight();
marker.setBounds(0, markerHeight, markerWidth, 0);
ResourceProxy resourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
myItemizedOverlay = new MyItemizedOverlay(marker, resourceProxy);
mapView.getOverlays().add(myItemizedOverlay);
GeoPoint myPoint1 = new GeoPoint(0*1000000, 0*1000000);
myItemizedOverlay.addItem(myPoint1, "myPoint1", "myPoint1");
GeoPoint myPoint2 = new GeoPoint(50*1000000, 50*1000000);
myItemizedOverlay.addItem(myPoint2, "myPoint2", "myPoint2");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="5dp"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<org.osmdroid.views.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"/>
</RelativeLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.openstreetmaptutorial"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.openstreetmaptutorial.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
清单:
public class MainActivity extends Activity {
MyItemizedOverlay myItemizedOverlay = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
Drawable marker=getResources().getDrawable(R.drawable.pin_for_map);
int markerWidth = marker.getIntrinsicWidth();
int markerHeight = marker.getIntrinsicHeight();
marker.setBounds(0, markerHeight, markerWidth, 0);
ResourceProxy resourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
myItemizedOverlay = new MyItemizedOverlay(marker, resourceProxy);
mapView.getOverlays().add(myItemizedOverlay);
GeoPoint myPoint1 = new GeoPoint(0*1000000, 0*1000000);
myItemizedOverlay.addItem(myPoint1, "myPoint1", "myPoint1");
GeoPoint myPoint2 = new GeoPoint(50*1000000, 50*1000000);
myItemizedOverlay.addItem(myPoint2, "myPoint2", "myPoint2");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="5dp"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<org.osmdroid.views.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"/>
</RelativeLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.openstreetmaptutorial"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.openstreetmaptutorial.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我希望输出类似于:
请建议我如何制作如上所述的弹出窗口您可以看看OSMBonusPack
更改MyItemizedOverlay
并使其扩展标记
类。然后,创建另一个自定义类,扩展MarkerInfoWindow
。以下是一些示例:
1.CustomMarker
class:
private static class CustomMarker extends Marker
{
public CustomMarker(MapView mapView, ResourceProxy resourceProxy) {
super(mapView, resourceProxy);
}
public CustomMarker(MapView mapView) {
super(mapView);
}
public String name;
public String desc;
}
CustomInfoWindow
class:
private static class CustomMarker extends Marker
{
public CustomMarker(MapView mapView, ResourceProxy resourceProxy) {
super(mapView, resourceProxy);
}
public CustomMarker(MapView mapView) {
super(mapView);
}
public String name;
public String desc;
}
公共类CustomInfoWindow扩展了MarkerInfoWindow{
private CustomMarker marker;
public CustomInfoWindow(MapView mapView) {
super(R.layout.bonuspack_bubble, mapView);
}
@Override
public void onOpen(Object item) {
marker = (CustomMarker) item;
Button btn = (Button) (mView.findViewById(R.id.bubble_moreinfo));
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (marker != null) {
if (marker.name!= null && marker.desc!= null) {
btn.setText(title);
btn.setDescription(desc);
}
}
}
});
}
}
在您的代码中的某个地方,然后:
public class MyClass extends Activity {
public MapView mapView;
@Override
protected void onCreate(Bundle load)
{
super.onCreate(load);
setContentView(R.layout.activity_map);
mapView = (MapView)findViewById(R.id.map);
}
....
CustomMarker marker = new CustomMarker(mapView);//you can also pass "this" as argument I believe
marker.setPosition(locatedGeoPoint);
marker.name = "nomnom";
marker.desc = "nomming";
marker.setInfoWindow(new CustomInfoWindow((mapView));
marker.setOnMarkerClickListener(new Marker.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker item, MapView arg1) {
item.showInfoWindow();
return true;
}
});
}
我在这里发布了一个类似的答案:
R.layout.activity\u map
是一个简单的XML布局,带有MapView
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.osmdroid.views.MapView
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
我已经解决了这个问题。首先,我创建了一个名为custom\u dialog.xml的布局。代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bonuspack_bubble">
<TextView
android:id="@+id/map_popup_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="3dp"
android:layout_marginTop="3dp"
android:text="TextView"
android:textSize="15dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/map_popup_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/map_popup_header"
android:layout_below="@+id/map_popup_header"
android:layout_marginTop="5dp"
android:text="TextView"
android:textSize="12dp"/>
<ImageView
android:id="@+id/map_more_info_imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/map_popup_body"
android:layout_marginRight="5dp"
android:src="@drawable/moreinfo_arrow" />
</RelativeLayout>
此方法应添加到主活动中
输出:
我尝试了这个方法。但是我无法正确使用CustomMarker和CustomInfoWindow类。找不到mapview。请您详细解释一下好吗?我很困惑,因为我是OSMI的新手,编辑了我的回复,以显示我是如何设置mapview的。OSMBonusPack也产生了问题。最新版本不支持正在编辑这个项目。你做了什么编辑?我不知道该说什么,我使用的是osmbonuspack_v4.4.jar版本。我自己编译了这个jar,来自源代码。非常感谢你的朋友。你救了我一天。这对我来说非常有效。