Baidu map development-mark multiple locations on the map, click the node pop-up PopupWindow, node popupwindow

Source: Internet
Author: User

Baidu map development-mark multiple locations on the map, click the node pop-up PopupWindow, node popupwindow

I recently wrote an android program and used some content of Baidu map API.


This DEMO is used to mark multiple locations on the map, and then click the node to pop up the PopupWindow


Below are some:





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 = "Location:" android: textSize = "20sp"/> <TextView android: id = "@ + id/TV _locationtime" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "time:" 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 extends; private Marker marker []; private TextView locationName; private TextView locationTime; private PopupWindow extends; describitmapdescriptor; describdground; @ 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 popupwindows (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 ("Qilu software Institute", "3.03.06.06", 36.673141, 117.114275 ); locations [1] = new LocationOfPhoto ("Tiananmen", "4.04.06.06", 39.912089, 116.403928); locations [2] = new LocationOfPhoto ("Fifth Avenue", "2013.04.02", 39.116073, 117.203932); locations [3] = new LocationOfPhoto ("Gansu Baiyin", "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 ("Location:" + locations [index]. getName (); locationTime. setText ("time:" + 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_longdistance = 0, max_latitude = 0, max_longdistance = 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 (). longlinks <= locations [I + 1]. getLocation (). longlinks) {min_longlinks = locations [I]. getLocation (). longpolling; max_longpolling = locations [I + 1]. getLocation (). longpolling;} else {min_longpolling = locations [I + 1]. getLocation (). longpolling; max_longpolling = locations [I]. getLocation (). longpolling ;}} 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 (longitude);} LatLng southwest = new LatLng (min_latitude, min_long.pdf); LatLng northeast = new LatLng (max_latitude, max_long.pdf); LatLng northwest = new LatLng (max_latitude, min_long.pdf ); latLng southeast = new LatLng (min_latitude, max_long.pdf); 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;}}



Why can't I click another widget after the pop-up window of PopupWindow in android

Because PopupWindow gets the focus and others do not get the focus, you cannot click it. Set setFocusable to false.
 
My mark on Baidu map will pop up and the data will be deleted after a while. How can I not be deleted?

Because you are not a Baidu employee, and the advertisement you mark is too high.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.