Easy drawing current location and compass on the MapView, using MyLocationOverlay.

Thảo luận trong 'Điện thoại, viễn thông' bắt đầu bởi bka15, 30/9/12.

  1. bka15 New Member

    bka15

    Tham gia ngày:
    18/7/12
    Bài viết:
    128
    Đã được thích:
    105
    Điểm thành tích:
    0
    com.google.android.maps provide a usefull class MyLocationOverlay to draw current location (and also compass) on the Map.


    [​IMG]
    To use MyLocationOverlay is simply:

    add a object of MyLocationOverlay in your mapView Overlays
    call enableMyLocation() and/or enableCompass() in onResume()
    call disableMyLocation() and/or disableCompass() onPause()
    add "android.permission.ACCESS_FINE_LOCATION" in AndroidManifest.xml



    The main.xml and MyItemizedOverlay.java have no change, you can copy the code from previous post Using ItemizedOverlay to add marker on MapView.

    AndroidMapViewActivity.java

    PHP:
    package com.AndroidMapView;
     
    import com.google.android.maps.GeoPoint;
    import com.google.android.maps.MapActivity;
    import com.google.android.maps.MapView;
    import com.google.android.maps.MyLocationOverlay;
     
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
     
    public class 
    AndroidMapViewActivity extends MapActivity {
      
     
    MyItemizedOverlay myItemizedOverlay null;
     
    MyLocationOverlay myLocationOverlay null;
      
       
    /** Called when the activity is first created. */
       
    @Override
       
    public void onCreate(Bundle savedInstanceState) {
           
    super.onCreate(savedInstanceState);
           
    setContentView(R.layout.main);
           
    MapView mapView = (MapViewfindViewById(R.id.mapview);
           
    mapView.setBuiltInZoomControls(true);
           
           
    Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
           
    int markerWidth marker.getIntrinsicWidth();
           
    int markerHeight marker.getIntrinsicHeight();
           
    marker.setBounds(0markerHeightmarkerWidth0);
           
           
    myItemizedOverlay = new MyItemizedOverlay(marker);
           
    mapView.getOverlays().add(myItemizedOverlay);
           
           
    GeoPoint myPoint1 = new GeoPoint(0*10000000*1000000);
           
    myItemizedOverlay.addItem(myPoint1"myPoint1""myPoint1");
           
    GeoPoint myPoint2 = new GeoPoint(50*100000050*1000000);
           
    myItemizedOverlay.addItem(myPoint2"myPoint2""myPoint2");
           
           
    myLocationOverlay = new MyLocationOverlay(thismapView);
           
    mapView.getOverlays().add(myLocationOverlay);
           
    mapView.postInvalidate();
     
       }
     
     @
    Override
     
    protected boolean isLocationDisplayed() {
      
    // TODO Auto-generated method stub
      
    return false;
     }
     
     @
    Override
     
    protected boolean isRouteDisplayed() {
      
    // TODO Auto-generated method stub
      
    return false;
     }
     
     @
    Override
     
    protected void onResume() {
      
    // TODO Auto-generated method stub
      
    super.onResume();
      
    myLocationOverlay.enableMyLocation();
      
    myLocationOverlay.enableCompass();
     }
     
     @
    Override
     
    protected void onPause() {
      
    // TODO Auto-generated method stub
      
    super.onPause();
      
    myLocationOverlay.disableMyLocation();
      
    myLocationOverlay.disableCompass();
     }
    }
    AndroidManifest.xml, "android.permission.INTERNET" and "android.permission.ACCESS_FINE_LOCATION" is needed.


    PHP:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
         package="com.AndroidMapView"
         android:versionCode="1"
         android:versionName="1.0">
       <uses-sdk android:minSdkVersion="4" />
       <application android:icon="@drawable/icon" android:label="@string/app_name">
        <uses-library android:name="com.google.android.maps" />
           <activity android:name=".AndroidMapViewActivity"
                     android:label="@string/app_name">
               <intent-filter>
                   <action android:name="android.intent.action.MAIN" />
                   <category android:name="android.intent.category.LAUNCHER" />
               </intent-filter>
           </activity>
       </application>
     <uses-permission android:name="android.permission.INTERNET"/>
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    </manifest>
    related: Implement own Custom MapView to move the routine works inside MapView.
     
    2 people like this.
    Đang tải...
  2. nguyenson.glhd New Member

    nguyenson.glhd

    Tham gia ngày:
    8/4/13
    Bài viết:
    31
    Đã được thích:
    0
    Điểm thành tích:
    0
    Giới tính:
    Nam
    Bài viết hay.
     

Chia sẻ trang này