0

我从这里获取 Google maps api v1 项目:http ://www.androidhive.info/2012/01/android-working-with-google-maps/

然后我想将其转换为 Google maps api v2。

AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidhive.googlemaps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15"
android:targetSdkVersion="15" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!--
     The following two permissions are not required to use
     Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <permission
        android:name="com.androidhive.googlemaps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.androidhive.googlemaps.permission.MAPS_RECEIVE" />
    <application
           android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >



        <activity
            android:label="@string/app_name"
            android:name=".AndroidGoogleMapsActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


         <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAts8OsbiR1Rje3EPz_nYwttbccAHXanL8" />
    </application>



</manifest>

AndroidGoogleMapsActivity.java:

   package com.androidhive.googlemaps;

    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.os.Bundle;

    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.MapFragment;


    import java.util.List;

    import android.graphics.drawable.Drawable;
    import android.os.Bundle;

    import com.google.android.maps.GeoPoint;
    import com.google.android.maps.MapActivity;
    import com.google.android.maps.MapController;
    import com.google.android.maps.MapView;
    import com.google.android.maps.Overlay;
    import com.google.android.maps.OverlayItem;

    public class AndroidGoogleMapsActivity extends Activity {
         GoogleMap map;
         @SuppressLint("NewApi")
         @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                    .getMap();
                  // map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                  // map.setMapType(GoogleMap.MAP_TYPE_NONE);
                  map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                  // map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                  // map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

            // Displaying Zooming controls
            MapView mapView = (MapView) findViewById(R.id.mapView);
            mapView.setBuiltInZoomControls(true);

            /**
             * Changing Map Type
             * */
            // mapView.setSatellite(true); // Satellite View
            // mapView.setStreetView(true); // Street View
            // mapView.setTraffic(true); // Traffic view

            /**
             * showing location by Latitude and Longitude
             * */        
            MapController mc = mapView.getController();
            double lat = Double.parseDouble("48.85827758964043");
            double lon = Double.parseDouble("2.294543981552124");
            GeoPoint geoPoint = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
            mc.animateTo(geoPoint);
            mc.setZoom(15);
            mapView.invalidate(); 


            /**
             * Placing Marker
             * */
            List<Overlay> mapOverlays = mapView.getOverlays();
            Drawable drawable = this.getResources().getDrawable(R.drawable.mark_red);
            AddItemizedOverlay itemizedOverlay = 
                 new AddItemizedOverlay(drawable, this);


            OverlayItem overlayitem = new OverlayItem(geoPoint, "Hello", "Sample Overlay item");

            itemizedOverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedOverlay);

        }



}

AddItemizedOverlay.java:

    package com.androidhive.googlemaps;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {

       private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

       private Context context;

       public AddItemizedOverlay(Drawable defaultMarker) {
            super(boundCenterBottom(defaultMarker));
       }

       public AddItemizedOverlay(Drawable defaultMarker, Context context) {
            this(defaultMarker);
            this.context = context;
       }

       @Override
       protected OverlayItem createItem(int i) {
          return mapOverlays.get(i);
       }

       @Override
       public int size() {
          return mapOverlays.size();
       }

       @Override
       protected boolean onTap(int index) {
          Log.e("Tap", "Tap Performed");
          return true;
       }

       public void addOverlay(OverlayItem overlay) {
          mapOverlays.add(overlay);
           this.populate();
       }

       /**
        * Getting Latitude and Longitude on Touch event
        * **/
       @Override
       public boolean onTouchEvent(MotionEvent event, MapView mapView) 
       {   

           if (event.getAction() == 1) {                
               GeoPoint geopoint = mapView.getProjection().fromPixels(
                   (int) event.getX(),
                   (int) event.getY());
               // latitude
               double lat = geopoint.getLatitudeE6() / 1E6;
               // longitude
               double lon = geopoint.getLongitudeE6() / 1E6;
               Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
           }                            
           return false;
       } 

    }

主.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mapView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true">
     <fragment  
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"
          />
</LinearLayout>

我在我的 Android 设备上运行,三星 S2 和 Android 4.0.3

安慰:

[2013-06-04 07:13:01 - AndroidGoogleMaps] Android Launch!
[2013-06-04 07:13:01 - AndroidGoogleMaps] adb is running normally.
[2013-06-04 07:13:01 - AndroidGoogleMaps] Performing com.androidhive.googlemaps.AndroidGoogleMapsActivity activity launch
[2013-06-04 07:13:01 - AndroidGoogleMaps] Uploading AndroidGoogleMaps.apk onto device '00195c3a6e376e'
[2013-06-04 07:13:01 - AndroidGoogleMaps] Installing AndroidGoogleMaps.apk...
[2013-06-04 07:13:04 - AndroidGoogleMaps] Success!
[2013-06-04 07:13:04 - AndroidGoogleMaps] Starting activity com.androidhive.googlemaps.AndroidGoogleMapsActivity on device 00195c3a6e376e
[2013-06-04 07:13:05 - AndroidGoogleMaps] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.androidhive.googlemaps/.AndroidGoogleMapsActivity }
[2013-06-04 07:16:47 - AndroidGoogleMaps]

之后,它显示“不幸的是 GoogleMaps 已停止”。

3天我努力解决这个问题,但没有解决。请帮我

4

2 回答 2

0

不需要使用public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem>

要添加标记,请参见粗体文本。

public class MapActivity extends Activity implements OnMarkerDragListener
{

    private GoogleMap map;
    Geocoder geocoder;
    Double lat,lon;
    static LatLng questionLoc=new LatLng(53.558, 9.927);
    Marker questionMarker;
    SharedPreferences mapPref;
    List<Address> addresses;
    MarkerOptions mark;
    String address,city,country,admin,subAdmin,location;
    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ask_map);
        Intent mapIntent=getIntent();
        lat=Double.parseDouble(mapIntent.getStringExtra("curr_lat"));
        lon=Double.parseDouble(mapIntent.getStringExtra("curr_long"));
        if(map==null)
            map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        map.setMyLocationEnabled(true);
        map.setOnMarkerDragListener(this);
        questionLoc=new LatLng(lat,lon);

mark= new MarkerOptions().position(questionLoc).title("Location").snippet(getIntent() .getStringExtra("Location")).draggable(true) .icon(BitmapDescriptorFactory.fromResource(R.drawable.mappin) );

    questionMarker = map.addMarker(mark);
    geocoder = new Geocoder(this, Locale.getDefault());
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(questionLoc, 15));
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2500, null);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    return true;
}

@Override
public void onMarkerDrag(Marker marker)
{
}

@Override
public void onMarkerDragEnd(Marker marker)
{

    try 
    {
        addresses = geocoder.getFromLocation(marker.getPosition().latitude,marker.getPosition().longitude, 1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    address = addresses.get(0).getAddressLine(0);
    Log.i("address map",""+addresses+"");
    city=addresses.get(0).getLocality();
    admin=addresses.get(0).getAdminArea();
    subAdmin=addresses.get(0).getSubAdminArea();
    Address obj = addresses.get(0);
    country=obj.getCountryName();

    if(city==null && admin== null && subAdmin==null)
    {
        location="";
        marker.setSnippet(location);
    }
    else
        location="temp";


    if(!location.equals(""))
    {

        questionLoc=questionMarker.getPosition();
        mapPref=getSharedPreferences(Constant.MAP_PREF, MODE_PRIVATE);
        SharedPreferences.Editor edit=mapPref.edit();
        edit.putString(Constant.MAP_LAT,""+questionLoc.latitude);
        edit.putString(Constant.MAP_LONG,""+questionLoc.longitude);
        edit.putString(Constant.MAP_COUNTRY,country);

        if(subAdmin!=null && admin!=null)
        {
            edit.putString(Constant.MAP_CITY,subAdmin);
            edit.putString(Constant.MAP_STATE,admin);
                location=subAdmin+" , "+admin;
        }
        else if(city!=null && admin!=null)
        {
            edit.putString(Constant.MAP_CITY,city);
            edit.putString(Constant.MAP_STATE,admin);
            location=city+" , "+admin;
        }
        else if(city==null && admin!=null)
        {
            edit.putString(Constant.MAP_CITY,admin);
            edit.putString(Constant.MAP_STATE,admin);
            location=admin;
        }
        else if(city==null && admin==null && subAdmin!=null)
        {
            edit.putString(Constant.MAP_CITY,subAdmin);
            edit.putString(Constant.MAP_STATE,subAdmin);
            location=subAdmin;
        }
        else if(city!=null && admin==null && subAdmin==null)
        {
            edit.putString(Constant.MAP_CITY,city);
            edit.putString(Constant.MAP_STATE,city);
            location=city;
        }
        edit.putString("Location", location);
        edit.commit();

        marker.setSnippet(location);
        marker.showInfoWindow();
    }
    else
    {
        marker.hideInfoWindow();
        Toast.makeText(this, "Please select proper location.", Toast.LENGTH_SHORT).show();
    }

}

@Override
public void onMarkerDragStart(Marker marker) {

}

}

于 2013-06-04T05:54:49.733 回答
0

从 v1 移植到 v2 没有简单的方法。

最好的办法是删除所有与地图相关的代码,并遵循地图 v2 的官方文档或找到自己的 v2 教程。

于 2013-06-04T05:44:53.417 回答