0

我在 Eclipse 中有一个使用 Api Key v1 的 Android 地图,现在我正在尝试将其转换为 v2。但是我很容易做到这一点。我做了一点努力,我得到了 Map Api 密钥 v2,并据此更新了我的 SDk,例如 Google Play 服务等,还据此更改了我的清单文件。现在是时候实际操作了更改了显示我的地图和来自 JSON Api 的标记的源代码。这是关于我的应用程序 布局文件的全部内容

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GMapsActivity" >

    <fragment
        android:id="@+id/map_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

    <EditText
        android:id="@+id/pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:width="150dp"
        android:gravity="center_horizontal"
        />

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/pass"
        android:layout_marginLeft="44dp"
        android:text="@string/log" />

</RelativeLayout>

显现

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="map3.pkg.pkg"
      android:versionCode="1"
      android:versionName="1.0"
      android:installLocation="auto">
    <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" />

    <permission android:name="map2.pkg.pkg.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
    <uses-feature android:glEsVersion="0x00020000"  android:required="true" />
    <uses-permission android:name="your.application.package.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application 
        android:allowBackup="true"
        android:icon="@drawable/icon" android:label="@string/app_name">
        <activity
            android:name="map3.pkg.pkg.GMapsActivity"
            android:label="@string/app_name" >
            <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="AIzaSyBxjGroO8h6KXAmxEGyTwR-MXqjLnYwU_k" />

        <uses-library android:name="com.google.android.maps" />
    </application>
</manifest> 

源文件

此代码实际上适用于 v1,现在我想将其转换为 v3

package map3.pkg.pkg;

import java.util.List;

//import android.R;
import android.graphics.drawable.Drawable;
import android.net.ParseException;
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;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import map2.pkg.pkg.R;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;

public class GMapsActivity extends MapActivity {

private MapView mapView;
EditText password;
Button   login;
TextView vwpass;

JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


password=(EditText)findViewById(R.id.pass);
login=(Button)findViewById(R.id.login);
mapView = (MapView) findViewById(R.id.map_view);       
        mapView.setBuiltInZoomControls(true);

        login.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {   
                if(password.getText().toString().equals("tsml")) {
                mapView.setVisibility(View.VISIBLE);
                login.setVisibility(View.INVISIBLE);
                password.setVisibility(View.INVISIBLE);
                } else {
                Toast.makeText(getBaseContext(), "invalid password - try again",  Toast.LENGTH_SHORT).show();
                }
            }
        });
      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
      try{
           HttpClient httpclient = new DefaultHttpClient();
           HttpPost httppost = new HttpPost("http://221.120.216.52/a/map.php");
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           HttpResponse response = httpclient.execute(httppost);
           HttpEntity entity = response.getEntity();
           is = entity.getContent();
           }catch(Exception e){
               Log.i("log_tag", "Error in http connection"+e.toString());
          }
      //convert response to string
      try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
             sb = new StringBuilder();
             sb.append(reader.readLine() + "\n");

             String line="0";
             while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
              }
              is.close();
              result=sb.toString();
              }catch(Exception e){
                    Log.e("log_tag", "Error converting result "+e.toString());
              }
//paring data
double LAT;
double LANG;
String INFO;
//result="[{\"lat\":\"31.5079616\",\"lang\":\"74.3376768\",\"info\":\" AQ Corolla (LZA 516)\",\"groupID\":\"Lahore\"},{\"lat\":\"31.5260192\",\"lang\":\"74.3768128\",\"info\":\" LC1 Corolla (LEA 6739)\",\"groupID\":\"Lahore\"}]";
try{
      jArray = new JSONArray(result);
      JSONObject json_data=null;
      for(int i=0;i<jArray.length();i++){
             json_data = jArray.getJSONObject(i);
             LAT=json_data.getDouble("lat");
             LANG=json_data.getDouble("lang");
             INFO=json_data.getString("info");


//Overlay code
List<Overlay> mapOverlays = mapView.getOverlays();
                Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
        CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this);

        //GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);
        GeoPoint point = new GeoPoint((int)(LAT * 1e6),(int)(LANG * 1e6));
        OverlayItem overlayitem = new OverlayItem(point, "Shakarganj", INFO);
        itemizedOverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedOverlay);

        MapController mapController = mapView.getController();

        mapController.animateTo(point);
        mapController.setZoom(6);

//Overlay code

         }
      }
      catch(JSONException e1){
          Toast.makeText(getBaseContext(), "No Vehicles Found" ,Toast.LENGTH_LONG).show();
      } catch (ParseException e1) {
            e1.printStackTrace();
    }
}
@Override
protected boolean isRouteDisplayed() {
    return false;
}
}

它只是从 URL 获取点并将其转换为字符串,然后使用 JSON 解析,然后在 Map.Plz 上显示。请任何人帮助我,我现在如何转换此代码以显示 api v3.Plz 的地图。

4

1 回答 1

4

如果您已经能够使用 google play 服务(Maps API V2)显示地图,那么您已经完成了很多工作。

其次,请有时间查看新的 Android Maps V2 API 的 Google 文档。它们很好地涵盖了大多数情况....

第三,您的 JSON 代码不受影响。就您而言,API 的最大变化是标记 mgmt。

您可以在向地图添加单个标记的位置更改此原始代码

List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this);

    //GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);
    GeoPoint point = new GeoPoint((int)(LAT * 1e6),(int)(LANG * 1e6));
    OverlayItem overlayitem = new OverlayItem(point, "Shakarganj", INFO);
    itemizedOverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedOverlay);

    MapController mapController = mapView.getController();

    mapController.animateTo(point);
    mapController.setZoom(6);

有了这个

LatLng markerPosition = new LatLng(LAT ,LANG );
googleMap.addMarker(new MarkerOptions().position(markerPosition
                         .title("Shakarganj")
                         .snippet(INFO));

不再需要使用叠加层。

如果要为标记设置动画,请在地图上使用 animateCamera 方法:

CameraPosition cameraPosition = new CameraPosition.Builder().target(markerPosition);
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

而不是使用 mapController。

于 2013-06-27T11:36:14.637 回答