当前位置:操作系统 > 安卓/Android >>

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.set

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,