Use mapView in android google map to adapt to multiple map identifiers

Source: Internet
Author: User

When you encounter a need, identify multiple locations (nationwide) in the map, and place the location gps in an xml file for modification at any time. The requirement is that the map should be as detailed as possible, but all locations must be included.
The procedure is as follows:
1. first obtain the maximum and minimum longitude and latitude of the data, and calculate the central point (also the longitude and latitude) of the data );
2. Set the map zoom level to a relatively large value by default, such as 19 or 20. In this case, many locations are not displayed on mapView, And the level value is gradually reduced to show all locations.
The process is clear and it seems that there is no problem. The main code is as follows:
Java code
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
ComList = this. getComList (); // get camera data
MapView = (MapView) findViewById (R. id. MapView01 );
MapView. setStreetView (true );
MapView. setEnabled (true );
MapView. setClickable (true );
MapView. setBuiltInZoomControls (true); // you can zoom in or out a map.

System. out. println ("length and width:" + mapView. getHeight () + "," + mapView. getWidth ());
MapController = (MapController) mapView. getController ();
// LocationManager = (LocationManager) getSystemService (Context. LOCATION_SERVICE); // Location Manager
 
// Set the map center point
Double lat1 = (min_y + max_y)/2*1E6;
Double lng1 = (min_x + max_x)/2*1E6;
System. out. println ("center point:" + lat1 + "," + lng1 );
GeoPoint geoPoint = new GeoPoint (lat1.intValue (), lng1.intValue ());
MapController. setCenter (geoPoint );
MapController. setZoom (16 );

...
 
Drawable defaultMarker = getResources (). getDrawable (R. drawable. arrow );
Drawable activeMarker = getResources (). getDrawable (R. drawable. icon );
DefaultMarker. setBounds (0, 0, defaultMarker. getMinimumWidth (),
DefaultMarker. getMinimumHeight ());
PositionsOverlay posOverlay = new PositionsOverlay (defaultMarker,
ComList, activeMarker, this );
List <Overlay> overlays = getMapView (). getOverlays ();
Overlays. add (posOverlay );


// Judge based on the upper left corner
Projection proj = mapView. getProjection ();
MapController mapController = mapView. getController ();
GeoPoint geoFromPix = proj. fromPixels (0, 0 );//
Double pix_lng = geoFromPix. getLongitudeE6 ()/1E6; // The gps longitude converted from the screen pixel in the upper left corner
Double pix_lat = geoFromPix. getLatitudeE6 ()/1E6; // The gps latitude converted from the screen pixel in the upper left corner
 
// System. out. println ("center before scaling:" + mapView. getMapCenter ());
While (min_x <pix_lng) {// if the minimum longitude in the data is smaller than the minimum longitude (outbound) corresponding to the screen, the map is reduced and the data is displayed on the screen.
System. out. println ("zoom out map 1:" + min_x + "," + pix_lng );
MapController. setZoom (mapView. getZoomLevel ()-1 );
Pix_lng = mapView. getProjection (). fromPixels (0, 0)
. GetLongitudeE6 ()/1E6;
Pix_lat = mapView. getProjection (). fromPixels (0, 0)
. GetLatitudeE6 ()/1E6;
 
}
While (max_y> pix_lat) {// if the maximum latitude of the data is greater than the minimum latitude (exclusive) corresponding to the screen, the map is reduced and the data is displayed on the screen.
System. out. println ("zoom in MAP 2 ");
MapController. setZoom (mapView. getZoomLevel ()-1 );
Pix_lat = mapView. getProjection (). fromPixels (0, 0)
. GetLatitudeE6 ()/1E6;
}


Running results, backend flooding:
Java code
08-18 07:47:00. 076: INFO/System. out (437): Zoom out map. 1004,121.145444
08-18 07:47:00. 076: INFO/System. out (437): Zoom out map. 1004,121.145444
08-18 07:47:00. 076: INFO/System. out (437): Zoom out map. 1004,121.145444
08-18 07:47:00. 076: INFO/System. out (437): Zoom out map. 1004,121.145444
08-18 07:47:00. 076: INFO/System. out (437): Zoom out map. 1004,121.145444
08-18 07:47:00. 076: INFO/System. out (437): Zoom out map. 1004,121.145444


Change the upper left corner to the lower right corner, and the width and height of mapView cannot be obtained...
Internet access is found. The statement is as follows:
Java code
A common mistake made by new Android developers is to use
The width and height of a view inside its constructor. When
A view's constructor is called, Android doesn't know yet how
Big the view will be, so the sizes are set to zero. The real sizes
Are calculated during the layout stage, which occurs after
Construction but before anything is drawn. You can use
OnSizeChanged () method to be notified of the values when they
Are known, or you can use the getWidth () and getHeight () methods
Later, such as in the onDraw () method

Http://www.coderanch.com/t/435390/Android/Mobile/screen-size-at-run-time
Do you have to wait for the layout to start? Use this statement
Java code
Pix_lng = mapView. getProjection (). fromPixels (0, 0)
. GetLongitudeE6 ()/1E6;

It's meaningless, too...
In this way, you can only process mapView after rendering.
I wanted to use the draw method of mapView for processing, but I had no idea (inherit the mapView class to override the draw method ?), I had to write a thread to monitor whether mapView is complete (if it is finished, neither height nor width will be equal to 0). Then I put the code just now into the thread and succeeded...
New thread code:

Java code
Public void run (){
While (mapView. getHeight () = 0 ){
System. out. println ("mapView is equal to 0, mapView has not been rendered ");
Try {
Thread. currentThread (). sleep (100 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
If (mapView. getHeight ()! = 0 ){
/*
* Use setZoom to adapt data labels to the screen
*/
 
// Judge based on the upper left corner
Projection proj = mapView. getProjection ();
MapController mapController = mapView. getController ();
GeoPoint geoFromPix = proj. fromPixels (0, 0 );//
Double pix_lng = geoFromPix. getLongitudeE6 ()/1E6; // The gps longitude converted from the screen pixel in the upper left corner
Double pix_lat = geoFromPix. getLatitudeE6 ()/1E6; // The gps latitude converted from the screen pixel in the upper left corner
 
// System. out. println ("center before scaling:" + mapView. getMapCenter ());
While (min_x <pix_lng) {// if the minimum longitude in the data is smaller than the minimum longitude (outbound) corresponding to the screen, the map is reduced and the data is displayed on the screen.
System. out. println ("zoom out map 1:" + min_x + "," + pix_lng );
MapController. setZoom (mapView. getZoomLevel ()-1 );
Pix_lng = mapView. getProjection (). fromPixels (0, 0)
. GetLongitudeE6 ()/1E6;
Pix_lat = mapView. getProjection (). fromPixels (0, 0)
. GetLatitudeE6 ()/1E6;
 
}
While (max_y> pix_lat) {// if the maximum latitude of the data is greater than the minimum latitude (exclusive) corresponding to the screen, the map is reduced and the data is displayed on the screen.
System. out. println ("zoom in MAP 2 ");
MapController. setZoom (mapView. getZoomLevel ()-1 );
Pix_lat = mapView. getProjection (). fromPixels (0, 0)
. GetLatitudeE6 ()/1E6;
}
System. out. println ("mapView value:" + mapView. getHeight ());
}
}

The Code was changed:
Java code
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
ComList = this. getComList (); // get camera data
MapView = (MapView) findViewById (R. id. MapView01 );
MapView. setStreetView (true );
MapView. setEnabled (true );
MapView. setClickable (true );
MapView. setBuiltInZoomControls (true); // you can zoom in or out a map.

System. out. println ("length and width:" + mapView. getHeight () + "," + mapView. getWidth ());
MapController = (MapController) mapView. getController ();
// LocationManager = (LocationManager) getSystemService (Context. LOCATION_SERVICE); // Location Manager
 
// Set the map center point
Double lat1 = (min_y + max_y)/2*1E6;
Double lng1 = (min_x + max_x)/2*1E6;
System. out. println ("center point:" + lat1 + "," + lng1 );
GeoPoint geoPoint = new GeoPoint (lat1.intValue (), lng1.intValue ());
MapController. setCenter (geoPoint );
MapController. setZoom (16 );

...
 
Drawable defaultMarker = getResources (). getDrawable (R. drawable. arrow );
Drawable activeMarker = getResources (). getDrawable (R. drawable. icon );
DefaultMarker. setBounds (0, 0, defaultMarker. getMinimumWidth (),
DefaultMarker. getMinimumHeight ());
PositionsOverlay posOverlay = new PositionsOverlay (defaultMarker,
ComList, activeMarker, this );
List <Overlay> overlays = getMapView (). getOverlays ();
Overlays. add (posOverlay );


// Start a new thread to check whether the rendering of the mapView is complete. After the rendering is complete, adjust the scaling level of the mapView to fit all the data.
SelfAdaptionMapView cv = new SelfAdaptionMapView (mapView, max_x, min_y, min_x, max_y );
New Thread (cv). start ();

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.