百度地圖開發-將多個地點標記在地圖上,點擊節點彈出PopupWindow,節點popupwindow
最近在寫一個安卓程式,用到了百度地圖API的一些內容,就隨便玩耍了一下。
這個DEMO是用來將多個地點標記在地圖上,然後點擊節點彈出PopupWindow
下面是一些:
main.xml
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.baidumaptest.MainActivity" > <LinearLayout android:id="@+id/ll_baidumap" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TextView android:id="@+id/tv_showinfo" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="fill_parent" android:layout_height="fill_parent" /></RelativeLayout>
popupwindow_showlocations.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#89D3D3D3" > <TextView android:id="@+id/tv_locationname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="地點:" android:textSize="20sp" /> <TextView android:id="@+id/tv_locationtime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="時間:" android:textSize="20sp" /> </LinearLayout>
MainActivity.java
import com.baidu.mapapi.SDKInitializer;import com.baidu.mapapi.map.BaiduMap;import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;import com.baidu.mapapi.map.BitmapDescriptor;import com.baidu.mapapi.map.BitmapDescriptorFactory;import com.baidu.mapapi.map.MapStatusUpdate;import com.baidu.mapapi.map.MapStatusUpdateFactory;import com.baidu.mapapi.map.MapView;import com.baidu.mapapi.map.Marker;import com.baidu.mapapi.map.MarkerOptions;import com.baidu.mapapi.map.OverlayOptions;import com.baidu.mapapi.model.LatLng;import com.baidu.mapapi.model.LatLngBounds;import android.support.v7.app.ActionBarActivity;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.PopupWindow;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends ActionBarActivity {private MapView mMapView;private BaiduMap mBaiduMap;private Marker marker[];private TextView locationName;private TextView locationTime;private PopupWindow infoPopupWindow;BitmapDescriptor bitmapDescriptor;BitmapDescriptor bdGround;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);SDKInitializer.initialize(getApplicationContext());setContentView(R.layout.layout_showlocations);locationName = (TextView)findViewById(R.id.tv_locationname);locationTime = (TextView)findViewById(R.id.tv_locationtime);LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);View popupWindow = layoutInflater.inflate(R.layout.popupwindow_showlocations, null);infoPopupWindow = new PopupWindow(popupWindow, 600, 800); infoPopupWindow.setFocusable(true);infoPopupWindow.setBackgroundDrawable(new BitmapDrawable());locationName = (TextView)popupWindow.findViewById(R.id.tv_locationname);locationTime = (TextView)popupWindow.findViewById(R.id.tv_locationtime);final LocationOfPhoto locations[] = new LocationOfPhoto[4];locations[0] = new LocationOfPhoto("齊魯軟體學院", "2013.06.06", 36.673141, 117.114275);locations[1] = new LocationOfPhoto("天安門", "2014.06.06", 39.912089, 116.403928);locations[2] = new LocationOfPhoto("五大道", "2013.04.02", 39.116073, 117.203932);locations[3] = new LocationOfPhoto("甘肅白銀", "2013.04.02", 36.501821, 104.205648);mMapView = (MapView)findViewById(R.id.bmapView);mBaiduMap = mMapView.getMap();bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);bdGround = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);initOverlay(locations);mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {@Overridepublic boolean onMarkerClick(Marker m) {for(int index = 0; index < locations.length; index++){if(m == marker[index]){locationName.setText("地點: "+locations[index].getName());locationTime.setText("時間: " +locations[index].getTime());Toast.makeText(getApplicationContext(), locations[index].getName()+"\n", Toast.LENGTH_LONG).show();infoPopupWindow.showAtLocation(mMapView, Gravity.CENTER, 0, 0);}}return true;}});}@Overridepublic 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;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}public void initOverlay(LocationOfPhoto locations[]){int count = locations.length;LatLng latLngs;LatLngBounds bounds = null;double min_latitude = 0, min_longitude = 0,max_latitude = 0, max_longitude = 0;for(int i = 0; i < count-1; i++){if(locations[i].getLocation().latitude <= locations[i+1].getLocation().latitude){min_latitude = locations[i].getLocation().latitude;max_latitude = locations[i+1].getLocation().latitude;}else {min_latitude = locations[i+1].getLocation().latitude;max_latitude = locations[i].getLocation().latitude;}if(locations[i].getLocation().longitude <= locations[i+1].getLocation().longitude){min_longitude = locations[i].getLocation().longitude;max_longitude = locations[i+1].getLocation().longitude;}else {min_longitude = locations[i+1].getLocation().longitude;max_longitude = locations[i].getLocation().longitude;}}marker = new Marker[count];for(int i = 0; i < count; i++){latLngs = locations[i].getLocation();OverlayOptions overlayOptions_marker = new MarkerOptions().position(latLngs).icon(bitmapDescriptor);marker[i] = (Marker)(mBaiduMap.addOverlay(overlayOptions_marker));}LatLng southwest = new LatLng(min_latitude, min_longitude);LatLng northeast = new LatLng(max_latitude, max_longitude);LatLng northwest = new LatLng(max_latitude, min_longitude);LatLng southeast = new LatLng(min_latitude, max_longitude);bounds = new LatLngBounds.Builder().include(northeast).include(southwest).include(southeast).include(northwest).build();MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLngBounds(bounds);mBaiduMap.animateMapStatus(mapStatusUpdate,1000);MapStatusUpdate mapStatusUpdate_zoom = MapStatusUpdateFactory.zoomTo(5);mBaiduMap.setMapStatus(mapStatusUpdate_zoom);}}
LocationOfPhoto.java
import com.baidu.mapapi.model.LatLng;public class LocationOfPhoto {private String name;private String time;private LatLng location;public LocationOfPhoto() {name = "";time = "";location = null;}public LocationOfPhoto(String name, String time, LatLng location){this.name = name;this.time = time;this.location = location;}public LocationOfPhoto(String name, String time, double latitude, double longitude){this.name = name;this.time = time;location = new LatLng(latitude, longitude);}/** * @return the name */public String getName() {return name;}/** * @param name the name to set */public void setName(String name) {this.name = name;}/** * @return the time */public String getTime() {return time;}/** * @param time the time to set */public void setTime(String time) {this.time = time;}/** * @return the location */public LatLng getLocation() {return location;}/** * @param location the location to set */public void setLocation(LatLng location) {this.location = location;}}
android中PopupWindow彈出表單後,為何不可以點擊其他控制項
因為PopupWindow獲得了焦點,其他得不到焦點,所以無法點擊,把setFocusable設為false就行了。
百度地圖 我做的標記,過一段時間就開啟就會彈出資料被刪除,怎才可以不被刪除
因為你不是百度工作人員,還有就是你標記的廣告嫌疑太大咯。