Add Tag and tipview for Android Google Map

Source: Internet
Author: User

1. Add a new mapoverlay to map. To plot the data point mark and tipview on mapoverlay:

Protected void adjustcacheinmap (final list <cachedata> caches,

Cachedata selectedcache ){
// Create tip View
Final view mtipview = minflater. Inflate (
R. layout. mobile_mc_findcache_result_map_tipview, null );

Mmapcaches. removeallviews ();
Mmapcaches. addview (mtipview );
Mmapcaches. Post (New runnable (){

@ Override
Public void run (){
Mmapcaches. getoverlays (). Clear ();
Mobileresultmapoverlay mapoverlay = new mobileresultmapoverlay (
Mobilefindcacheresultactivity. This, mtipview, null,
Caches, mvibrator, mmapcontroller );
Mmapcaches. getoverlays (). Add (mapoverlay );
Mmapcaches. getoverlays (). Add (Mlo );
Mmapcaches. invalidate ();
}
});
}

2. assumeresultmapoverlay is as follows:

Package com. MC. yellowbook. Android. Map;

Import java. util. arraylist;
Import java. util. List;

Import Android. App. activity;
Import Android. content. intent;
Import Android. Graphics. Bitmap;
Import Android. Graphics. bitmapfactory;
Import Android. Graphics. Canvas;
Import Android. Graphics. matrix;
Import Android. Graphics. Point;
Import Android. OS. vibrator;
Import Android. View. view;
Import Android. widget. imageview;
Import Android. widget. textview;

Import com. Google. Android. Maps. geopoint;
Import com. Google. Android. Maps. mapcontroller;
Import com. Google. Android. Maps. mapview;
Import com. Google. Android. Maps. overlay;
Import com. Google. Android. Maps. projection;
Import com. MC. yellowbook. Android. R;
Import com. MC. yellowbook. Android. App. appconstants;
Import com. MC. yellowbook. Android. App. apputils;
Import com. MC. yellowbook. Android. findcache. mobilefindviewcacheactivity;
Import com. MC. yellowbook. Android. model. businessdata;
Import com. MC. yellowbook. Android. model. cachedata;

Public class mobileresultmapoverlay extends overlay implements
Android. View. View. onclicklistener {
// Offset for tip View
Private Static int tip_position_x_offerset = 12;
Private Static int tip_position_y_offerset = 7;

Private bitmap geopnt = NULL;
Private cachedata selected = NULL;
Private list <cachedata> coll = NULL;
Private list <geopoint> points = NULL;
Private activity;
Private vibrator;
Private mapcontroller;

Private view mtipview;
Private textview mtextmaptipcachename, mtextmaptipbusinessname,
Mtextmaptipdistance;
Private imageview mimgmaptipviewcache, mimgmaptipcachedifficulty,
Mimgmaptipcachesize;

Public assumeresultmapoverlay (activity, view mtipview,
Cachedata selectedcache, list <cachedata> caches, vibrator, mapcontroller ){
Super ();
This. mapcontroller = mapcontroller;
This. vibrator = vibrator;
This. Activity = activity;
This. mtipview = mtipview;
Coll = caches;

If (selectedcache = NULL & caches! = NULL & caches. Size ()! = 0 ){
// Set the first one selected by default
Selected = caches. Get (0 );
}

Points = new arraylist <geopoint> ();
Final list <cachedata> pointslist = new arraylist <cachedata> ();
For (cachedata data: Coll ){
Businessdata P = data. getbusiness ();
If (P = NULL ){
Continue;
}
If (P. getlate6 () = 0 & P. getlone6 () = 0 ){
Continue;
}

Geopoint PNT = new geopoint (P. getlate6 (), P. getlone6 ());
Points. Add (PNT );
Pointslist. Add (data );
}

If (mtipview! = NULL ){
// Get view by ID
Mtextmaptipbusinessname = (textview) mtipview
. Findviewbyid (R. Id. textresultmaptipbusinessname );
Mtextmaptipcachename = (textview) mtipview
. Findviewbyid (R. Id. textresultmaptipcachename );
Mtextmaptipdistance = (textview) mtipview
. Findviewbyid (R. Id. textresultmaptipdistance );
Mimgmaptipviewcache = (imageview) mtipview
. Findviewbyid (R. Id. imgresultmaptipviewcache );
Mimgmaptipcachedifficulty = (imageview) mtipview
. Findviewbyid (R. Id. imgresultmaptipdiff );
Mimgmaptipcachesize = (imageview) mtipview
. Findviewbyid (R. Id. imgresultmaptipsize );

// Set click listener
Mimgmaptipviewcache. setonclicklistener (this );
}

// Get bitmap for point show on Map
Geopnt = bitmapfactory. decoderesource (activity. getresources (),
R. drawable. mc_cache_flag );
}

@ Override
Public Boolean ONTAP (geopoint click, mapview ){

Projection projection = mapview. getprojection ();

Point tappoint = projection. topixels (click, null );

Point mypoint = NULL;
Int idx = 0;
Double min_dis = math. Pow (25.0, 2 );
Cachedata cache = NULL;
For (cachedata data: Coll ){
Businessdata P = data. getbusiness ();
If (P = NULL | (P. getlate6 () = 0 & P. getlone6 () = 0 )){
Continue;
}

Mypoint = projection. topixels (points. Get (idx), null );
Double Dis = math. Pow (tappoint. X-mypoint. X, 2.0)
+ Math. Pow (tappoint. Y-mypoint. Y, 2.0 );
If (DIS <min_dis ){
Min_dis = DIS;
Cache = data;
}
Idx ++;
}

// If (Cache! = NULL ){
If (Cache! = Selected ){
Selected = cache;
// Mtipview. setvisibility (view. Visible );
}
//}

If (selected! = NULL ){
// Move selected cache to map center
Businessdata selectedbusiness = selected. getbusiness ();
Mapcontroller. animateto (New geopoint (selectedbusiness. getlate6 (),
Selectedbusiness. getlone6 ()));
}

Return false;
}

@ Override
Public Boolean draw (canvas, mapview, Boolean shadow,
Long when ){
Projection projection = mapview. getprojection ();
Matrix matrix = new matrix ();

Float scale = 0.7f;
Matrix. postscale (scale, scale );
Int width = geopnt. getwidth ();
Int Height = geopnt. getheight ();
Bitmap newgeopnt = NULL;
Newgeopnt = bitmap. createbitmap (geopnt, 0, 0, width, height, matrix,
True );
Width = newgeopnt. getwidth ();
Height = newgeopnt. getheight ();

// Draw all points
Int idx = 0;
For (cachedata data: Coll ){
Businessdata P = data. getbusiness ();
If (P = NULL | (P. getlate6 () = 0 & P. getlone6 () = 0 )){
Continue;
}

Point point = new point ();
Projection. topixels (points. Get (idx), point );
Canvas. drawbitmap (newgeopnt, point. X-width, point. Y-height,
Null );
Idx ++;
}

// Show tip information
If (selected! = NULL ){
Point point = new point ();
Projection. topixels (New geopoint (selected. getbusiness (). getlate6 (),
Selected. getbusiness (). getlone6 (), point );

// Show the information in the tip.
Mapview. layoutparams Pos = (mapview. layoutparams) mtipview
. Getlayoutparams ();
Geopoint geop = new geopoint (selected. getbusiness (). getlate6 (),
Selected. getbusiness (). getlone6 ());
POs. Point = geop;
POs. x =-width/2 + tip_position_x_offerset;
POs. Y =-height-mtipview. getheight ()/2 + tip_position_y_offerset;
Mtipview. setlayoutparams (POS );

// Set the text
Mtextmaptipbusinessname. settext (selected. getbusiness (). getname ());
Mtextmaptipcachename. settext (selected. gettitle ());
Mtextmaptipdistance. settext (selected. getbusiness (). getonedecimaldistance ());
Mimgmaptipcachedifficulty. setimagelevel (selected. getdifficulty ());
Mimgmaptipcachesize. setimagelevel (selected. getsize ());

Mtipview. setvisibility (view. Visible );
Mtipview. bringtofront ();
} Else {
Mtipview. setvisibility (view. Gone );
}

Return super. Draw (canvas, mapview, shadow, when );
}

@ Override
Public void onclick (view v ){
Apputils. vibrate (vibrator );

If (V = mimgmaptipviewcache ){
// Go to View cache page
Intent I = new intent (activity, mobilefindviewcacheactivity. Class );
I. putextra (appconstants. intent_extra_cache_detail,
Selected );
Activity. startactivity (I );
}
}
}

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.