MapMarkerExample

This example show you how to draw movable marker on to map. You can darg and drop the marker to change its position. Can be used in any application where u want to take input from user for map location.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it MapMarkerExample.
2.) You will see some default code into your main.xml and android manifest file.
3.) Add internet permission to your manifest file or write following in android.manifest file:
Xml代碼    
1. <?xml version="1.0" encoding="utf-8"?> 
2. <manifest package="com.example.mapmarker" xmlns:android="http://schemas.android.com/apk/res/android" 
3.         android:versionCode="1" android:versionName="1.0"> 
4.         <uses-sdk android:minSdkVersion="7" /> 
5.         <uses-permission android:name="android.permission.INTERNET" /> 
6.         <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
7.         <application android:icon="@drawable/icon" android:label="@string/app_name"> 
8.                 <uses-library android:name="com.google.android.maps" /> 
9.                 <activity android:label="@string/app_name" android:name=".MapMarkerActivity"> 
10.                         <intent-filter> 
11.                                 <action android:name="android.intent.action.MAIN" /> 
12.                                 <category android:name="android.intent.category.LAUNCHER" /> 
13.                         </intent-filter> 
14.                 </activity> 
15.         </application> 
16. </manifest> 

4.) Write following into main.xml file:
Xml代碼    
1. <?xml version="1.0" encoding="utf-8"?> 
2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
3.         android:layout_width="wrap_content" android:layout_height="wrap_content"> 
4.         <com.google.android.maps.MapView 
5.                 android:id="@+id/map" android:layout_width="wrap_content" 
6.                 android:layout_height="wrap_content" android:apiKey="0jp8vWjNayJISFKdvcJwGmwsjgoCoQrT_dflCfQ" 
7.                 android:clickable="true" /> 
8.         <ImageView android:id="@+id/drag" android:layout_width="wrap_content" 
9.                 android:layout_height="wrap_content" android:src="@drawable/marker" 
10.                 android:visibility="gone" /> 
11. </RelativeLayout> 

5.) CreateYour own map API key and replace it in above main.xml.
6.) Add any marker image or push pin image in drawable folder.
7.) Run for output.
Steps:
1.) Create a project named MapMarkerExample and set the information as stated in the image.
Build Target: Android 2.1 (Google API)
Application Name: MapMarkerExample
Package Name: com.example.mapmarker
Activity Name: MapMarkerActivity
Min SDK Version: 7

2.) Open MapMarkerActivity.java file and write following code there:
Java代碼    
1. package com.example.mapmarker;  
2. import java.util.ArrayList;  
3. import java.util.List;  
4. import android.graphics.Canvas;  
5. import android.graphics.Point;  
6. import android.graphics.drawable.Drawable;  
7. import android.os.Bundle;  
8. import android.view.KeyEvent;  
9. import android.view.MotionEvent;  
10. import android.view.View;  
11. import android.widget.ImageView;  
12. import android.widget.RelativeLayout;  
13. import android.widget.Toast;  
14. import com.google.android.maps.GeoPoint;  
15. import com.google.android.maps.ItemizedOverlay;  
16. import com.google.android.maps.MapActivity;  
17. import com.google.android.maps.MapView;  
18. import com.google.android.maps.MyLocationOverlay;  
19. import com.google.android.maps.OverlayItem;  
20. public class MapMarkerActivity extends MapActivity {  
21.   private MapView map=null;  
22.   private MyLocationOverlay me=null;  
23.     
24.   @Override 
25.   public void onCreate(Bundle savedInstanceState) {  
26.     super.onCreate(savedInstanceState);  
27.     setContentView(R.layout.main);  
28.       
29.     map=(MapView)findViewById(R.id.map);  
30.       
31.     map.getController().setCenter(getPoint(21.0000169992044,  
32.                                             78.0000484771729));  
33.     map.setBuiltInZoomControls(true);  
34.      
35.       
36.     Drawable marker=getResources().getDrawable(R.drawable.marker);  
37.       
38.     marker.setBounds(0, 0, marker.getIntrinsicWidth(),marker.getIntrinsicHeight());  
39.       
40.     map.getOverlays().add(new SitesOverlay(marker));  
41.       
42.     me=new MyLocationOverlay(this, map);  
43.     map.getOverlays().add(me);  
44.   }  
45.     
46.   @Override 
47.   public void onResume() {  
48.     super.onResume();  
49.       
50.     me.enableCompass();  
51.   }     
52.     
53.   @Override 
54.   public void onPause() {  
55.     super.onPause();  
56.       
57.     me.disableCompass();  
58.   }     
59.     
60.   @Override 
61.   protected boolean isRouteDisplayed() {  
62.     return(false);  
63.   }  
64.     
65.   @Override 
66.   public boolean onKeyDown(int keyCode, KeyEvent event) {  
67.     if (keyCode == KeyEvent.KEYCODE_S) {  
68.       map.setSatellite(!map.isSatellite());  
69.       return(true);  
70.     }  
71.     else if (keyCode == KeyEvent.KEYCODE_Z) {  
72.       map.displayZoomControls(true);  
73.       return(true);  
74.     }  
75.       
76.     return(super.onKeyDown(keyCode, event));  
77.   }  
78.   private GeoPoint getPoint(double lat, double lon) {  
79.     return(new GeoPoint((int)(lat*1000000.0),  
80.                           (int)(lon*1000000.0)));  
81.   }  
82.       
83.   private class SitesOverlay extends ItemizedOverlay<OverlayItem> {  
84.     private List<OverlayItem> items=new ArrayList<OverlayItem>();  
85.     private Drawable marker=null;  
86.     private OverlayItem inDrag=null;  
87.     private ImageView dragImage=null;  
88.     private int xDragImageOffset=0;  
89.     private int yDragImageOffset=0;  
90.     private int xDragTouchOffset=0;  
91.     private int yDragTouchOffset=0;  
92.       
93.     public SitesOverlay(Drawable marker) {  
94.       super(marker);  
95.       this.marker=marker;  
96.         
97.       dragImage=(ImageView)findViewById(R.id.drag);  
98.       xDragImageOffset=dragImage.getDrawable().getIntrinsicWidth()/2;  
99.       yDragImageOffset=dragImage.getDrawable().getIntrinsicHeight();  
100.         
101.       items.add(new OverlayItem(getPoint(21.169992044,  
102.                                                                 78.484771729),  
103.                                 "Mumbai",  
104.                                 "Maharashtra, India"));  
105.       populate();  
106.     }  
107.       
108.     @Override 
109.     protected OverlayItem createItem(int i) {  
110.       return(items.get(i));  
111.     }  
112.       
113.     @Override 
114.     public void draw(Canvas canvas, MapView mapView,  
115.                       boolean shadow) {  
116.       super.draw(canvas, mapView, shadow);  
117.         
118.       boundCenterBottom(marker);  
119.     }  
120.       
121.     @Override 
122.     public int size() {  
123.       return(items.size());  
124.     }  
125.       
126.     @Override 
127.     public boolean onTouchEvent(MotionEvent event, MapView mapView) {  
128.       final int action=event.getAction();  
129.       final int x=(int)event.getX();  
130.       final int y=(int)event.getY();  
131.       boolean result=false;  
132.         
133.       if (action==MotionEvent.ACTION_DOWN) {  
134.         for (OverlayItem item : items) {  
135.           Point p=new Point(0,0);  
136.             
137.           map.getProjection().toPixels(item.getPoint(), p);  
138.             
139.           if (hitTest(item, marker, x-p.x, y-p.y)) {  
140.             result=true;  
141.             inDrag=item;  
142.             items.remove(inDrag);  
143.             populate();  
144.             xDragTouchOffset=0;  
145.             yDragTouchOffset=0;  
146.               
147.             setDragImagePosition(p.x, p.y);  
148.             dragImage.setVisibility(View.VISIBLE);  
149.             xDragTouchOffset=x-p.x;  
150.             yDragTouchOffset=y-p.y;  
151.               
152.             break;  
153.           }  
154.         }  
155.       }  
156.       else if (action==MotionEvent.ACTION_MOVE && inDrag!=null) {  
157.         setDragImagePosition(x, y);  
158.         result=true;  
159.       }  
160.       else if (action==MotionEvent.ACTION_UP && inDrag!=null) {  
161.         dragImage.setVisibility(View.GONE);  
162.           
163.         GeoPoint pt=map.getProjection().fromPixels(x-xDragTouchOffset,  
164.                                                    y-yDragTouchOffset);  
165.         OverlayItem toDrop=new OverlayItem(pt, inDrag.getTitle(),  
166.                                            inDrag.getSnippet());  
167.         Toast.makeText(MapMarkerActivity.this, pt.getLatitudeE6()+" "+pt.getLongitudeE6(), Toast.LENGTH_SHORT).show();  
168.         items.add(toDrop);  
169.         populate();  
170.           
171.         inDrag=null;  
172.         result=true;  
173.       }  
174.         
175.       return(result || super.onTouchEvent(event, mapView));  
176.     }  
177.       
178.     private void setDragImagePosition(int x, int y) {  
179.       RelativeLayout.LayoutParams lp=  
180.         (RelativeLayout.LayoutParams)dragImage.getLayoutParams();  
181.               
182.       lp.setMargins(x-xDragImageOffset-xDragTouchOffset,  
183.                       y-yDragImageOffset-yDragTouchOffset, 0, 0);  
184.       dragImage.setLayoutParams(lp);  
185.     }  
186.   }  
187. } 

3.) Compile and build the project.
4.) Move the marker shown on the map with the help of mouse for results shown below.
Output

摘自 yi_17328214

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。