首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将android地图v1转换为google android map v2

将android地图v1转换为google android map v2
EN

Stack Overflow用户
提问于 2013-06-27 10:43:09
回答 1查看 2.3K关注 0票数 0

我在Eclipse中有一个使用Api键v1的Android,现在我正在尝试将它转换为v2。但是我完全糊涂了,我做了一点努力,我得到了Map键v2,并根据etc服务等更新了My SDk,还根据that.Now更改了我的报表文件,是时候修改我的源代码了,它显示了我的地图,而JSON Api.Here中的标记都是关于我的应用程序布局文件的。

代码语言:javascript
复制
<?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>

流形

代码语言:javascript
复制
<?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

代码语言:javascript
复制
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上显示

EN

回答 1

Stack Overflow用户

发布于 2013-06-27 11:36:14

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

第二,请你看看新安卓地图V2 API的Google文档的时间。它们涵盖了大多数案件.

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

您可以更改原始代码,在该代码中要向地图添加一个标记。

代码语言:javascript
复制
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);

有了这个

代码语言:javascript
复制
LatLng markerPosition = new LatLng(LAT ,LANG );
googleMap.addMarker(new MarkerOptions().position(markerPosition
                         .title("Shakarganj")
                         .snippet(INFO));

不再需要与覆盖一起工作了。

如果要动画到标记,可以在地图上使用animateCamera方法:

代码语言:javascript
复制
CameraPosition cameraPosition = new CameraPosition.Builder().target(markerPosition);
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

而不是使用mapController。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17341014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档